Home > front end >  Flutter http.get send OPTIONS instead GET when compiled to Web or Andriod
Flutter http.get send OPTIONS instead GET when compiled to Web or Andriod

Time:05-05

I am trying to get data from a REST endpoint in Flutter. However, when it goes to the endpoint it shows up as OPTIONS instead of GET. Doing more testing, the code below does working when I set the device to Windows (desktop), but Chrom Web, and Android does not work.

Am I doing something wrong or is there a correct way to force a GET? Here is my code:

var url = Uri.https('mywebsite', '/api/v1/MyEndpoint');
final response = await http.get(
    url,
    headers: {
      HttpHeaders.authorizationHeader:
          'Bearer $token',
      HttpHeaders.contentTypeHeader: 'application/json'
    },
  );

Looking at the logs of the endpoint shows this:

Request starting HTTP/1.1 OPTIONS https://mywebsite/api/v1/MyEndpoint - 0 Executing endpoint '405 HTTP Method Not Supported'

Flutter returns the error:

Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 910:28                get current
packages/http/src/browser_client.dart 69:22                                                                                    <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1685:54                                              runUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 159:18                                        handleValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 766:44                                        handleValueCallback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 795:13                                        _propagateToListeners
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/future_impl.dart 592:7                                         [_complete]
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream_pipe.dart 61:11                                         _cancelAndValue
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/stream.dart 1288:7                                             <fn>
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14  _checkAndCall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39  dcall
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/html/dart2js/html_dart2js.dart 37301:58                              <fn>
    at Object.createErrorWithStack (http://localhost:51989/dart_sdk.js:5080:12)
    at Function._throw (http://localhost:51989/dart_sdk.js:20337:18)
    at Function.throwWithStackTrace (http://localhost:51989/dart_sdk.js:20334:18)
    at async._AsyncCallbackEntry.new.callback (http://localhost:51989/dart_sdk.js:40851:18)
    at Object._microtaskLoop (http://localhost:51989/dart_sdk.js:40708:13)
    at _startMicrotaskLoop (http://localhost:51989/dart_sdk.js:40714:13)
    at http://localhost:51989/dart_sdk.js:36191:9

Bonus Question: why is the error message in XMLHttpRequest? I am asking for JSON.

Note: the endpoint does in fact work, I can hit from Postman, and here is what a successful log looks like:

Request starting HTTP/1.1 GET https://mywebsite/api/v1/MyEndpoint - 0 
Successfully validated the token. 
//Logging of the Request

EDIT: Discovered that it works correctly when compiling on for Windows but does not work when sending to Andriod or Web.

CodePudding user response:

Look for CORS (Cross-Origin Resource Sharing), the browser sends the OPTIONS request first when the URL is different from the current one. For example, when you are debugging your browser URL is localhost:PORT, but your website is different. You'll have to fix CORS handling on your website for this connection to work. PS: Test it out by sending OPTIONS with Postman.

  • Related