【发布时间】:2023-04-09 19:30:01
【问题描述】:
我想发送这样的数据:
{
"ad_title": "test",
"year": "2019",
"class": "Highline",
"files": [ 212]
}
Flutter 中的 api。我试着这样做
var data = {
"year": selectedYear,
"class": _classController.text.toString(),
"files": json.encode(carfilesids),
};
final response = await http.post(url, body: data,headers:
{'Accept': 'application/json'});
但是在运行时没有错误,当我打印 json.encoder.convert(carfilesids) 的值时,它给了我列表,我得到一个响应状态代码 422,它告诉我“{”message“:”给定的数据无效。","errors":{"files":["文件必须是数组。"]}}"。我也尝试过 json.encode 整个数据,但它与其他数据属性出错。
【问题讨论】:
-
问题来自你的服务器,检查它是否以正确的方式读取数据。
-
您还需要对数据进行 JSON 编码。将 body 参数从
body: data替换为body: json.encode(data)而不需要json.encodefiles。 -
我尝试为文件和数据添加 json.encode 但没有成功