【问题标题】:Flutter reports error "Cannot access the body fields of a Request"Flutter 报错“Cannot access the body fields of a Request”
【发布时间】:2020-11-16 16:07:55
【问题描述】:

错误:

无法访问没有内容类型的请求的正文字段 "应用程序/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),
                            ),
                          ),

【问题讨论】:

标签: flutter dart flutter-layout flutter-dependencies flutter-http


【解决方案1】:

你应该像下面的例子一样传递 header 对象:

  // 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(),
    };

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

【讨论】:

  • 你好,@SanketVekariya 端点不是 SubmitDailyStockTaking 但是这个 StockItem/UpdateStockItem 我尝试了上面的例子,但它仍然没有工作先生
  • 我可以知道代码中for循环的目的是什么吗?意思是你到底想达到什么目标?
  • 好的,@SanketVekariya 循环将项目更新到数据库。这些项目可以是一个或多个。
  • 原因是您的“oid”和“unitInStock”是 int 类型的数据,您必须传递与此数据类型不同的内容。我已经通过这些参数传递了这个提到的数据类型并得到响应 200。请检查数据类型!
  • 好的 @SanketVekariya 谢谢你让我检查一下你使用整数作为两个或字符串,拜托。如果有效,我可以看看你做了什么吗?我标记你,先生?
猜你喜欢
  • 1970-01-01
  • 2020-08-12
  • 2022-12-27
  • 2023-02-11
  • 2022-12-27
  • 2022-12-01
  • 2021-07-05
  • 2022-12-27
  • 2017-11-16
相关资源
最近更新 更多