我正在尝试将json对象发送到api端点。但我得到了以下信息:
I / flutter(28184): 200
I / flutter(28184): {"error": "the JSON object must be str, not 'bytes'"}
我尝试过以下几种方法:
Future<http.Response> sendPost() async{
var post = {
"profile_id" : "${_profile_id}",
"profile_name" : "${_profile_Name}",
"profile_country" : "${_profile_country}",
"post_id" : "${current_post_id}",
"post_image_id" : "${current_post_image}",
"post_desc" : "La La La La La La",
"post_likes" : "${post_likes}",
"post_timestamp" : "05-06-2019 22:34",
"post_comments" : [],
"post_comments_profile_name" : [],
"post_comments_profile_image_id" : [],
"post_comments_timestamp" : []
};
var body = utf8.encode(json.encode(post));
var addPost = await http.post(
url,
headers: {"content-type" : "application/json"},
body: body
);
print("${addPost.statusCode}");
print("${addPost.body}");
return addPost;
}
另外,当您回答时,请记住此Map中应该有空数组。
发布于 2019-05-15 06:16:55
不需要转换地图:
var body = utf8.encode(json.encode(post)); // delete this
只需像这样将贴图发送到身体:
var addPost = await http.post(
url,
body: post
);
发布于 2019-05-15 10:32:00
为了将JSON发送到body中,您应该将JSON串起来。
var body = JSON.stringify(post);
我认为不需要编码。
发布于 2019-06-23 18:01:22
不要使用utf8.encode()转换贴图。因为JSON对象必须是字符串而不是字节。直接发送地图:
var addPost = await http.post(
url,
body: post
);
我从评论中了解到,您希望在将来检索字符串数组。不要担心容易实现的目标:
https://stackoverflow.com/questions/56139284
复制相似问题