0

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> 
1
  • Adding CORS headers to a request does nothing! It's the server that needs to add the CORS headers in its responses. Commented May 20, 2022 at 0:59

1 Answer 1

2

Encode body to JSON.

body: jsonEncode(sendString),

See Sending data to server for details.

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

2 Comments

Thank you so much but now I have another error : XMLHttpRequest error.

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.