You need to enable JavaScript to run this app. Flutter发送含对象列表的HTTP POST请求时业务返回码与Postman不一致
优惠活动
大模型
产品
解决方案
定价
更多

Flutter发送含对象列表的HTTP POST请求时业务返回码与Postman不一致

Flutter POST接口业务返回码异常求助
  • 已成功获取服务器200 HTTP响应状态码,但接口返回的业务响应码为0
  • 使用Postman发送相同请求时返回的业务码为1,怀疑是请求体中提交的JSON内容存在缺失
  • 本人是Flutter开发新手,需要发送请求体为对象列表的HTTP POST请求,目标请求体格式如下,恳请各位提供帮助:
[
    {
        "product_id": 14,
        "quantity": 3,
        "payment": "COD"
    },
    {
        "product_id": 3,
        "quantity": 2,
        "payment": "COD"
    }
]

已编写的相关代码

1. 自定义HTTP POST请求函数

Future<dynamic> checkItem({Map headers, body, encoding}) async {
    Map<String, String> headers = {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': 'Bearer $token'
    };
    try {
      final response = await http.post(
          '${_url}transactions/check',
          headers: headers,
          body: body,
          encoding: Encoding.getByName("utf-8"));
      if (response.statusCode == 200) {
        String data = response.body;
        return jsonDecode(data);
      } else {
        print(response.statusCode);
      }
    } catch (error) {
      print(error);
    }
  }

2. 调用请求、传递JSON请求体的代码

List<String> chckList = checkoutList.map((e) => json.encode(e.toJson())).toList();
String strBody = json.encode(chckList);
final res = await interface.checkOutItem(body: strBody);

3. Model对象的toJson序列化方法

Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['product_id'] = this.product_id;
    data['quantity'] = this.quantity;
    data['payment'] = this.payment;
    return data;
  }

内容的提问来源于stack exchange,提问作者Jayrek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.12.01 22:36:04