This is a complete example. You have to use json.encode(...)
to convert the body of your request to JSON.
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';
var url = "https://someurl/here";
var body = json.encode({"foo": "bar"});
Map<String,String> headers = {
'Content-type' : 'application/json',
'Accept': 'application/json',
};
final response =
http.post(url, body: body, headers: headers);
final responseJson = json.decode(response.body);
print(responseJson);
Generally it is advisable to use a Future
for your requests so you can try something like
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:io';
Future<http.Response> requestMethod() async {
var url = "https://someurl/here";
var body = json.encode({"foo": "bar"});
Map<String,String> headers = {
'Content-type' : 'application/json',
'Accept': 'application/json',
};
final response =
await http.post(url, body: body, headers: headers);
final responseJson = json.decode(response.body);
print(responseJson);
return response;
}
The only difference in syntax being the async
and await
keywords.