如何以原始 JSON 格式发送 http

How to send http in raw JSON format

嗨,我有一个问题 这些是我的 http body 和 header 我想将其转换为请求邮递员请求的预期请求

其中格式为 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

如果有人能帮助我,我需要找到解决方案,在此先感谢

试试这个希望对你有帮助

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"
                  }
                ]
              });

可能以后肯定会有人遇到这个问题,不用担心

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() 中使用 wrap body 并在 header 中使用 "Content-Type": "application/json" 并且奇迹发生了。