Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Post data to json api using flutter

I’m working on a flutter project and I want to add data to json api using this code :

save() async {
    try{
    var sendString =
        {
        'userId': userId,
        'userEmail': userEmail,
      };
    final response = await http.post(
      Uri.parse('http://localhost:5000/createapp'),
      headers: <String, String>{
        "ContentType": 'application/json; charset=UTF-8',
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": "true",
        "Access-Control-Allow-Headers": "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,locale",
        "Access-Control-Allow-Methods": "POST, OPTIONS",
      },
      body: sendString,
    );
    print(response.body);
      }catch(e){
      print(e);
    }
  }

But it gave me this error in my flutter console I don’t know what’s the problem, any help is highly appreciated :

Posting data...
<!doctype html>
<html lang=en>
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p> 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Encode body to JSON.

body: jsonEncode(sendString),

See Sending data to server for details.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading