如何在 http POST 请求正文中发送数组

How to send an array in http POST request body

我有一组对象要添加到我的购物车中,我必须在我的 POST 请求中将其作为 body 参数发送,如下所示:

[
    {"value":74,"key":"product_attribute_53_12_25"},
    {"value":3,"key":"product_attribute_1_6_2"},
    {"value":6,"key":"product_attribute_1_3_3"},
    {"value":8,"key":"product_attribute_1_4_4"},
    {"value":10,"key":"product_attribute_1_8_5"},
    {"value":5,"key":"addtocart_53.EnteredQuantity"}
]

这是我的服务:

Future<AddToCartModelResponse> addToCartService() async {
  AddToCartModelResponse model = AddToCartModelResponse();
  Uri uri = Uri.parse(Constants.baseUrl + "AddProductToCart/53/1");
  Map<String, String> header = {
    "NST": Constants.NST,
    "Content-Type": "application/json",
  };
  var body = {
    //???????????
  };
  try {
    var response = await http.post(uri, body: body, headers: header);
    print(response.body);
    if (response.statusCode == 200) {
      model = AddToCartModelResponse.fromJson(json.decode(response.body));
      return model;
    } else {
      model = AddToCartModelResponse.fromJson(json.decode(response.body));
      model.statusCode = response.statusCode;
      return model;
    }
  } on TimeoutException catch (error, stack) {
    print('error time');
    model.statusCode = 500;
    return model;
  } catch (error, stack) {
    print(error);
    model.statusCode = 500;
    return model;
  }
}

这里,uri设置了参数,还有我的body参数来自productDetail,像这样:

[ {"key":"product_attribute_{productId}{attributeId}{Id}", "value":{attrValueId}},
{"value":{quantity}, "key":"addtocart_{productId}.EnteredQuantity"} ]

A​​ body 可以是 MapList:

Future<AddToCartModelResponse> addToCartService() async {
  AddToCartModelResponse model = AddToCartModelResponse();
  Uri uri = Uri.parse(Constants.baseUrl + "AddProductToCart/53/1");
  Map<String, String> header = {
    "NST": Constants.NST,
    "Content-Type": "application/json",
  };
  var body = [
    {"value":74,"key":"product_attribute_53_12_25"},
    {"value":3,"key":"product_attribute_1_6_2"},
    {"value":6,"key":"product_attribute_1_3_3"},
    {"value":8,"key":"product_attribute_1_4_4"},
    {"value":10,"key":"product_attribute_1_8_5"},
    {"value":5,"key":"addtocart_53.EnteredQuantity"}
   ];
  try {
    var response = await http.post(uri, body: body, headers: header);
    print(response.body);
    if (response.statusCode == 200) {
      model = AddToCartModelResponse.fromJson(json.decode(response.body));
      return model;
    } else {
      model = AddToCartModelResponse.fromJson(json.decode(response.body));
      model.statusCode = response.statusCode;
      return model;
    }
  } on TimeoutException catch (error, stack) {
    print('error time');
    model.statusCode = 500;
    return model;
  } catch (error, stack) {
    print(error);
    model.statusCode = 500;
    return model;
  }
}

或者,如果您从模特身上取 body:


class Cart {
 List<Product> products;

 List<Map<String, dynamic>> toJson(){
   return products.map((p)=>p.toJson()).toList();
 }
}

/*...*/

Future<AddToCartModelResponse> addToCartService(Cart cart) async {
  /*...*/
  var body = cart.toJson();
  /*...*/
}