I have a json api. Like this 'https://myapiurl.com/api.json'. I can read data in this api and I can fetch data in my app. But i can't post data in the my api. Here my source code.
I am fetching data this way.
Future<List<Stock>> getStock() async {
var response = await http.get(Uri.parse("https://myapiurl.com/api.json"));
var _stocks = <Stock>[];
_stocks = (json.decode(utf8.decode(response.bodyBytes)) as List)
.map((singleStockMap) => Stock.fromJson(singleStockMap))
.toList();
return _stocks;
}
And i am trying this way for post data.
postData() async {
try {
var response = await http
.post(Uri.parse("https://myapiurl.com/api.json"), body: {
"productName": 1.toString(),
"productStock": "Hello",
});
print(response.body);
} catch (e) {
print(e);
}
}
What is wrong?