-3

错误:

无法访问没有内容类型“application/x-www-form-urlencoded”的请求的正文字段

我已尽我所能,但由于此错误,我仍然无法发出 PUT HTTP 请求。

在此处输入图像描述

我也试过这个:

headers: {'Content-Type': 'application/x-www-form-urlencoded'},

它仍然无法正常工作。

这是代码:

RaisedButton(
                            padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                            color: Colors.green,
                            child: Text(
                              "Submit and Validate",
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold,
                                  fontSize: 12),
                            ),
                            onPressed: () async {
                              // widget.stockoid
                              var jsonbody = {
                                "oid": mainStockid,
                                "modifiedBy": userData['UserName'],
                                "modifiedOn": DateTime.now().toString(),
                                "submitted": true,
                                "submittedOn": DateTime.now().toString(),
                                "submittedBy": userData['UserName'].toString(),
                              };
                              for (Products i in selectedProducts) {
                                print('${i.oid.toString()} itemid');
                                //i.oid.toString(),
                                var stockupdate = {
                                  "oid": i.oid,
                                  "unitInStock": i.itemqty,
                                  "modifiedBy": userData['UserName'].toString(),
                                  "modifiedOn": DateTime.now().toString(),
                                };
                                
                               //But I could print this data on the console 
                               print(
                                    '${i.oid}, ${i.itemqty.toString()},${userData['UserName']}, 
                                ${DateTime.now().toString()} ploo');
                                await http
                                    .put(
                                        "http://api.ergagro.com:112/StockItem/UpdateStockItem",
                                        headers: {
                                          'Content-Type': 'application/json'
                                        },
                                        body: jsonEncode(stockupdate))
                                  //The value returns 404 witht that error  
                                  .then((value) {
                                  print(value.statusCode);
                                });
                              }
                              await http
                                  .put(
                                      'http://api.ergagro.com:112/SubmitDailyStockTaking',
                                      headers: {
                                        'Content-Type': 'application/json'
                                      },
                                      body: jsonEncode(jsonbody))
                                  .then((value) {
                                print('${value.statusCode} subb');
                              });
                              Navigator.push(
                                  context,
                                  new MaterialPageRoute(
                                      builder: (context) =>
                                          StartScanPage(widget.dcOid)));
                              //Send to API
                            },
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(300),
                            ),
                          ),
4

1 回答 1

0

您应该像下面的示例一样传递标头对象:

  // set up PUT request arguments
  String url = 'http://api.ergagro.com:112/SubmitDailyStockTaking';
  Map<String, String> headers = {
      "Content-type": "application/json"
  };
  String json = jsonEncode(stockupdate);  
  
  // make PUT request
  Response response = await put(url, headers: headers, body: json);  
  ...

更新:

“oid”和“unitInStock”的输入参数必须是整数数据类型。
为了更好的理解,

更改以下代码:

var stockupdate = {
      "oid": i.oid,
      "unitInStock": i.itemqty,
      "modifiedBy": userData['UserName'].toString(),
      "modifiedOn": DateTime.now().toString(),
};

有以下一个:

var stockupdate = {
          "oid": 123,
          "unitInStock": 2,
          "modifiedBy": "UserName",
          "modifiedOn": DateTime.now().toString(),
    };

或基于相关数据类型的值(整数)。

于 2020-07-27T11:22:20.270 回答