我想将数据发布到postgres服务器。我需要从注册表中发送信息以及一些图片。所以我需要使用multipart / form-data。事情是我的注册表很复杂,有几个嵌套的对象(下面我将粘贴其中的一部分,以便您可以看到我在说什么)。
在这里,经过两天的研究,我尝试了不同的选择,但我无法使其正常工作。无论使用http还是Dio软件包,似乎都在Dio软件包FormData.fromMap()中将其格式化不正确...
有什么办法可以使它正常工作吗?或者我已经要求后端的人为两个不同的请求分别注册表格和图片,并将其作为json发送...
这是我表单的一部分:
FormData.fromMap({
"questionnaire": {
"horse": {"name": "Abisynia", "breed": "demo", "sex": "male"},
"owner": {
"personal_data": "John Doe",
"email": "asasdasss@gmail.com",
"phone_number": "3556234432"
},
"breakfast": {"name": "morning", "manufacturer": "Adasa", "amount": 1},
"dinner": {"name": "noon", "manufacturer": "Aasd", "amount": 2},
"supper": {"name": "evening", "manufacturer": "ASds", "amount": 3}
}
})
这是我通过邮递员发帖(并且有效!)时从服务器发送的日志:
以下是我尝试通过我的应用发布信息时的日志:
任何想法我都会感激不尽...
答案 0 :(得分:0)
在将嵌套的 json 传递给 FormData 之前对其进行编码。它会起作用。例如:
String horseJson = json.encode({"name": "Abisynia", "breed": "demo", "sex": "male"});
String ownerJson = json.encode({
"personal_data": "John Doe",
"email": "asasdasss@gmail.com",
"phone_number": "3556234432"
});
String breakfastJson = json.encode({"name": "morning", "manufacturer": "Adasa", "amount": 1});
String dinnerJson = json.encode({"name": "noon", "manufacturer": "Aasd", "amount": 2});
String supperJson = json.encode({"name": "evening", "manufacturer": "ASds", "amount": 3});
String questiinnaireJson = json.encode({
"horse": horseJson,
"owner": ownerJson,
"breakfast": breakfastJson,
"dinner": dinnerJson,
"supper": supperJson
});
FormData formData = FormData.fromMap({
"questionnaire": questiinnaireJson
});
var response = await dio.post(url, data: formData, options: Options(headers:_headers));