Home > front end >  Post data to json api using flutter
Post data to json api using flutter

Time:05-20

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> 

CodePudding user response:

Encode body to JSON.

body: jsonEncode(sendString),

See Sending data to server for details.

  • Related