首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Flutter中使用http.post并作为String Json对象发送

如何在Flutter中使用http.post并作为String Json对象发送
EN

Stack Overflow用户
提问于 2019-05-15 06:09:11
回答 3查看 563关注 0票数 0

我正在尝试将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中应该有空数组。

EN

回答 3

Stack Overflow用户

发布于 2019-05-15 06:16:55

不需要转换地图:

var body = utf8.encode(json.encode(post)); // delete this

只需像这样将贴图发送到身体:

var addPost = await http.post(
    url,
    body: post
);
票数 0
EN

Stack Overflow用户

发布于 2019-05-15 10:32:00

为了将JSON发送到body中,您应该将JSON串起来。

var body = JSON.stringify(post);

我认为不需要编码。

票数 0
EN

Stack Overflow用户

发布于 2019-06-23 18:01:22

不要使用utf8.encode()转换贴图。因为JSON对象必须是字符串而不是字节。直接发送地图:

var addPost = await http.post(
    url,
    body: post
);

我从评论中了解到,您希望在将来检索字符串数组。不要担心容易实现的目标:

链接相同:Flutter how to convert String to List

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56139284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档