I'm trying to pass a Json body inside a Json body using flutter. The function is as follow:
Future<String> postItem(Item item, Brand brand) async {
var jsonob = jsonEncode(<String, dynamic>{
'item_code': item.itemCode,
'item_name': item.itemName,
'part_no': item.partNo,
'min_stock': int.parse(item.minStock),
'brand': jsonEncode(brand)
});
print(jsonob); //for debugging
final http.Response response = await http.post(
BASE_URL + "item",
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonob
);
Brand the following function to encode to json:
Map<String, dynamic> toJson() => {
"id": int.parse(this.brandId),
"brand_name": this.brandName,
"brand_code": this.brandCode,
"brand_image": this.brandImage
};
error message:
E/flutter ( 1601): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: {error: entity validation failed, reasons: [invalid input type for 'brand']}
output of print for jsonob:
{"item_code":"item-001","item_name":"item123","part_no":"part123","min_stock":1,"brand":"{\"id\":1,\"brand_name\":\"BRANDA\",\"brand_code\":\"BRANDA\",\"brand_image\":\"null\"}"}
I'm using the guidelines from here: https://flutter.dev/docs/cookbook/networking/send-data
The http server is created using Aqueduct. I tried running the same http.post using POSTMAN and there was no error thrown, so I'm unsure what is causing this error in flutter.
Any help would be greatly appreciated.
i think request body should encoded to a Json string using JsonEncode(Object) so it should be that way :