I need to send a GET request to an API from my flutter application, with json parameters in the body. I've googled it (maybe I've not googled it so well), But a lot of people have this problem of sending a GET request with JSON body in flutter. When I try the request in POSTMAN it work.
var url = 'linkofapi;
var response = await http.get(url);
See, first of all, GET
operations are not intended to send the data. It is meant to receive the data, hence the method name GET
, hence http.get()
doesn't have a body
.
If sending the data is the utmost requirement, then you need to use Uri
to add a query parameter
to the http
method.
Please read about Uri.http flutter for more information.
You can do something like this to achieve what you want:
// Please note "...." for more information, please do not use this as is
// Just wanted to give a heads up
final query = {
'name': your_name,
'email': your_email,
'password': your_password
...
};
var url = 'linkofapi;
// Now you use the query to pass it to. your get method
final uri = Uri.http(url, '/path', query);
// adding headers to the query
final header = {HttpHeaders.contentTypeHeader: 'application/json'};
// doing the operation finally
final response = await http.get(uri, headers: header);
Try this
Future<http.Response> createUser({name, email, password, gender, dob}) {
return http.get(
'http://$BASE_API_URL/users',
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'name': name,
'email': email,
'password': password,
'gender': gender,
'dob': dob,
}),
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.