post request complex json to api in flutter
369
Just Do it like json.encode(myArr)
,
for example
Map<String, String> params = {
"mobile": userNameController.text,
"password": passwordController.text,
"deviceid": '${fcmToken}',
};
Future<String> postRequest(var url, {var postParams}) async {
return http
.post(url, body: json.encode(postParams))// do json encoding here
.then((http.Response response) {
final int statusCode = response.statusCode;
print("postParams " + json.encode(postParams));
print("statusCode " + '${statusCode} ${response.body}');
if (statusCode < 200 || statusCode > 400 || json == null) {
throw new Exception("Error while fetching data");
}
print(response.request);
return response.body;
});
}
Author by
Sarah
Updated on December 15, 2022Comments
-
Sarah over 1 year
i have this json i want to post to an api but i am not sure how it is done ..this is how the request looks like :
{ "products": [ {"id":1, "qty": 1},{"id":2, "qty": 1} ], "vendor_id": 1, "notes": " ", "address": "" }
and this is the request class that i used to map the request :
class Order{ int vendor_id; String address ,notes ; List<OrderProduct> products; Order({this.products , this.address , this.notes , this.vendor_id}); Map<String, dynamic> toMap() { return { 'vendor_id': vendor_id, 'address': address, 'notes': notes, 'products': products, }; } } class OrderProduct{ int id , qty ; OrderProduct({this.id , this.qty}); Map<String, dynamic> toMap() { return { 'id': id, 'qty': qty, }; } }
what am i missing out ?