Reputation: 225
i have been working on flutter project which accepts name and phone number of users but when i save it it shows response 500 but from post man its working fine.
but from flutter here is the code
void RegisterUsers(String names, String phone) async{
String urlInser = "https://hmmmiii.com/api/addUser";
Map<String, String> headers = {
'Content-Type': 'application/json',
//'Accept': 'application/json',
};
final msg = jsonEncode({"Name":"$names","PhoneNumber":"$phone"});
var response = await http.post(urlInser,
headers: headers,
body: msg);
print(response.statusCode);
if (response.statusCode == 200) {
print('User Registered');
} else {
print('Unsuccessful');
}
where names and phone are textController values thanks.
Upvotes: 2
Views: 1028
Reputation: 1059
in postman, you are sending the request as form-data, but in your code, you are sending it as a simple JSON.
you have to use MultipartRequest
instead.
final url = 'your url';
final request = http.MultipartRequest('POST', Uri.parse(url))
..fields['Name'] = 'some name'
..fields['PhoneNumber'] = 'some phonenumber';
final response = await request.send();
final responseBody = await response.stream.bytesToString();
print('RESPONSE BODY: $responseBody');
Upvotes: 2