flutter - Flutter 报错“Cannot access the body fields of a Request”
问题描述
错误:
无法访问没有内容类型“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),
),
),
解决方案
您应该像下面的示例一样传递标头对象:
// 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(),
};
或基于相关数据类型的值(整数)。
推荐阅读
- python - 没有包的 Python 中的序列整数矩阵
- haskell - Haskell、PatternSynonyms、COMPLETE pragma 和 View Patterns
- python - 如何通过 Python 代码运行 Powershell 并运行 .exe 文件?
- python - 我应该如何优化我的代码?- 从排序的链表中删除重复值节点
- java - AlarmManager 无法调用我的 BroadcastReceiver
- spring-boot - 如何在多个 spring-boot 微服务模块之间共享“UserContext”?
- apache-spark - 如何选择 Pyspark 列并将其作为新行附加到数据框中?
- java - Android - Firebase RTDB 检索和显示数据
- r - 根据年龄计算出生日期
- android - 使用 Google 搜索小部件时,CountDownTimer 在 Android 应用程序内停止?