如何在Postman中以原始JSON格式发送HTTP请求

vcirk6k6  于 2023-08-05  发布在  Postman
关注(0)|答案(2)|浏览(151)

下面是我的JSON的HTTP body和header。我想将其转换为Postman请求的预期格式,其中格式为JSON和“raw”:

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"
  },

字符串
但是当我运行这个的时候,我得到了这个错误:
类型“List<Map<String,String>>”不是类型强制转换中类型“String”的子类型

jk9hmnmh

jk9hmnmh1#

试试这个,希望对你有帮助

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

字符串

hgb9j2n6

hgb9j2n62#

也许将来有人会为他们面对这个问题,别担心

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",然后奇迹发生了。

相关问题