0

Making http post request fails on flutter web. I am using http package for making request here is my code:

http.post(
      'http://x.x.x.x:5000',
      headers: <String, String>{
        "Access-Control-Allow-Origin": "*", // Required for CORS support to work
        "Access-Control-Allow-Credentials":
            'true', // Required for cookies, authorization headers with HTTPS
        "Access-Control-Allow-Headers":
            "Origin,Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
        "Access-Control-Allow-Methods": "POST, OPTIONS"
      },
      body: jsonEncode(<String, String>{
        'title': 'title',
      }),
    )

Error: XMLHttpRequest error. dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 906:28 get current packages/http/src/browser_client.dart 84:22 dart-sdk/lib/async/zone.dart 1450:54 runUnary dart-sdk/lib/async/future_impl.dart 143:18 handleValue dart-sdk/lib/async/future_impl.dart 696:44 handleValueCallback

Note: In chrome Dev tools I can see 2 requests going through one is my post request and I don't know about the other one. The other one is always successful with 200 status and is logged at server but that isn't my post request.

2 Answers 2

2

After wasting hours looking for a solution I have found that I haven't implemented CORS on server side. I was using Flask server and adding CORS quickly resolved the issue.

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

Comments

0

This is a server-side issue.

Enable CORS in Cpanel to enable CORS in your hosting account. you can enable it by adding the following lines in the .htaccess file in your hosting account.

<IfModule mod_headers. ... Header set Access-Control-Allow-Origin "*"

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.