I am sending the following POST request to my node.js server:
import 'package:dio/dio.dart';
import 'dart:io';
print("--- userData: $userData"); // its a Map<String, dynamic>
Dio dio = new Dio();
dio.options.headers = {
'Authorization': 'Bearer ' + prefs.getString("TOKEN"),
'uuid': _uuid
};
dio.options.contentType = ContentType.parse("application/json");
FormData formData = new FormData.from(userData);
print("--- updated user info: $formData");
Response response = await dio.post("https://myapi.com/api/user/update", data: formData);
if(response.statusCode == 200) {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("User Info Updated"),
));
} else {
Map<String, dynamic> _responseMap = json.decode(response.data);
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text(_responseMap['message']),
));
}
where the userData and formData print statement prints out something like this:
{
first_name: John,
last_name: Doe,
age: 56,
gender: Male,
phone: 8888888888,
email: [email protected],
others: some text and some other stuff
}
However on my server side I get an empty body. However when I do the same thing with Flutter's http:
Map<String, String> headersMap = {
'Content-Type' : 'application/json',
'Authorization' : 'Bearer ' + prefs.getString("TOKEN"),
'uuid': _uuid
};
http.post(
'https://myapi.com/api/user/update',
body: json.encode(userData),
headers: headersMap
).then((http.Response response){
if(response.statusCode == 200) {
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text("User Info Updated"),
));
}
});
I receive the data on server side.
I even tried the Dio code by commenting the dio.options.contentType line but I still get empty request body. What is going on? What am I missing?
you should send post request directly as follows:
Response response = await dio.post("https://myapi.com/api/user/update", data: userData);
In your situation, you shouldn't use FormData,because using FormData will lead the dio to set request contentType as "multipart/form-data"。
Thanks, it worked.
you should send post request directly as follows:
Response response = await dio.post("https://myapi.com/api/user/update", data: userData);In your situation, you shouldn't use
FormData,because usingFormDatawill lead the dio to set requestcontentTypeas "multipart/form-data"。
But what about the case of sending file to the server using post request
you should send post request directly as follows:
Response response = await dio.post("https://myapi.com/api/user/update", data: userData);In your situation, you shouldn't use
FormData,because usingFormDatawill lead the dio to set requestcontentTypeas "multipart/form-data"。But what about the case of sending file to the server using post request
u can use FormData but don't specify a content type if u specified a content type the body will be empty I tried it and it worked that way
Most helpful comment
you should send post request directly as follows:
In your situation, you shouldn't use
FormData,because usingFormDatawill lead the dio to set requestcontentTypeas "multipart/form-data"。