Flutter 报错 "Cannot access the body fields of a Request"
Flutter reports error "Cannot access the body fields of a Request"
错误:
Cannot access the body fields of a Request without content-type
"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),
),
),
你应该通过 header object 像下面的例子:
// 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(),
};
或基于相关数据类型的值(整数)。
错误:
Cannot access the body fields of a Request without content-type "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),
),
),
你应该通过 header object 像下面的例子:
// 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(),
};
或基于相关数据类型的值(整数)。