Flutter reports error "Cannot access the body fields of a Request"

Issue

Error:

Cannot access the body fields of a Request without content-type
"application/x-www-form-urlencoded"

I have tried the best I can but still I cannot make a PUT HTTP Request because of this error.

enter image description here

I have also tried this:

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

And it still it couldn’t work.

Here is the code:

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),
                            ),
                          ),

Solution

You should pass header object like below example:

  // 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);  
  ...

Update:

The input parameter of “oid” and “unitInStock” must be an integer data typed.
For better understanding,

change below code:

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

with below one:

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

or relevant datatype based values (integer).

Answered By – Sanket Vekariya

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *