Search code examples
flutterdarthttp-post

How to send an array in http POST request body


I have an array of objects to add to my cart which I have to send as body parameter in my POST request, something like this:

[
    {"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"}
]

this is my service:

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;
  }
}

here, uri set with parameters, and also my body parameters come from productDetail like this:

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


Solution

  • A body can either be a Map or a List:

    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;
      }
    }
    

    Or if your taking body from a model:

    
    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();
      /*...*/
    }