【问题标题】:How to send http in raw JSON format如何以原始 JSON 格式发送 http
【发布时间】:2020-12-24 10:38:24
【问题描述】:

您好,我有一个问题 这些是我的 http 正文和标头,我想将其转换为邮递员请求的预期请求

其中格式为 JSON 格式

var response = await http.post(
      '${UIData.baseAppUrl}/api/v1/endpoint',
body: {
    "device_id": "$deviceId",
    "country_id": "$countryId",
    "contacts": [
      {
        "name": "test",
        "email": "test@test.com",
        "phone": "+911111111110"
      },
      {
        "name": "Test 2",
        "email": "test2@test2.com",
        "phone": "+910000000000"
      }
    ]
  },
  headers: {
    "Accept": "application/json",
    "Authorization": "Bearer $token"
  },

但是当我运行这个时,我会得到这个

type 'List<Map<String, String>>' is not a subtype of type 'String' in type cast

如果有人可以帮助我,请提前找到解决方案,谢谢

【问题讨论】:

    标签: flutter http networking https http-headers


    【解决方案1】:

    可能将来有人肯定会遇到这个问题,因为他们不用担心

    var response = await http.post(
          '${UIData.baseAppUrl}/api/v1/endpoint',
    body: jsonEncode({
        "device_id": "$deviceId",
        "country_id": "$countryId",
        "contacts": [
          {
            "name": "test",
            "email": "test@test.com",
            "phone": "+911111111110"
          },
          {
            "name": "Test 2",
            "email": "test2@test2.com",
            "phone": "+910000000000"
          }
        ]
      }),
      headers: {
        "Accept": "application/json",
        "Authorization": "Bearer $token",
        "Content-Type": "application/json",
      },
    

    我只是在jsonEncode() 中使用包裹体并在标题中使用"Content-Type": "application/json",然后奇迹发生了。

    【讨论】:

      【解决方案2】:

      试试这个希望对你有帮助

      body: json.encode(
                    {
                      "device_id": "$deviceId",
                      "country_id": "$countryId",
                      "contacts": [
                        {
                          "name": "test",
                          "email": "test@test.com",
                          "phone": "+911111111110"
                        },
                        {
                          "name": "Test 2",
                          "email": "test2@test2.com",
                          "phone": "+910000000000"
                        }
                      ]
                    });
      

      【讨论】:

      • 所以我需要在后端@Boffin Coders 上解码它吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 2012-04-02
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多