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.

Mateen Kiani
  • 2,869
  • 2
  • 19
  • 29

2 Answers2

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.

Mateen Kiani
  • 2,869
  • 2
  • 19
  • 29
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 "*"

Aniket
  • 441
  • 5
  • 8