2

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. enter image description here

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.

1 Answer 1

2

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');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.