dio icon indicating copy to clipboard operation
dio copied to clipboard

Cannot read set-cookie from response header in Flutter Web

Open yerim1210 opened this issue 5 years ago • 45 comments

According to #585, cookies are automatically managed by the browser, but they are not in Flutter Web. (They works well in Android/iOS). I can see a set-cookie value from the response header in the browser dev tap, but they are not saved in Cookies Storage and not appeared on the console when I print all the headers from the response, it only shows like the below. [Response Headers] {cache-control: no-cache, no-store, max-age=0, must-revalidate, content-type: application/json; charset=utf-8, expires: -1, pragma: no-cache}

I also tried the this code from @whimthen but it is not working for me.

There are quite many people experiencing this issue and no certain workaround. I'd appreciate there would be an exact workaround.

yerim1210 avatar Jan 12 '21 09:01 yerim1210

I'm facing the same issue.

joo avatar Mar 10 '21 15:03 joo

We are seeing the same issue, using dio 3.0.10. Interestingly we have a header that the srver responds with, X-Authentication that we can see in the network tab of chrome is in the response but the header does not show up in the response headers in dio. Is there a white list of headers we need to add to to allow this header to be made available to us?

I've read somewhere that chrome is consuming the set-cookie header and it is managing the cookie, which makes some sense, but does not explain why this X-Authorisation header is being consumed. Any ideas?

gbloggs avatar Mar 28 '21 12:03 gbloggs

我也遇到了同样的问题

xia-weiyang avatar Apr 03 '21 08:04 xia-weiyang

Having the same problem any update?

sivaram16 avatar Apr 12 '21 10:04 sivaram16

I'm facing the same issue.

CZXBigBrother avatar Apr 16 '21 05:04 CZXBigBrother

Facing the same issue, any solution?

ibrahimsoliman97 avatar Apr 16 '21 12:04 ibrahimsoliman97

Just an update on this...

dio 4.0.0
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.0.5, on Microsoft Windows [Version 10.0.19042.928], locale en-GB)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] Android Studio
[√] Connected device (4 available)

Running on web, hitting the same endpoint as an the android app. Both generated from the same flutter codebase generate these headers in response to the exact same RESTful call:

Web:

headers: {cache-control: [no-cache,  no-store,  max-age=0,  must-revalidate], expires: [0], pragma: [no-cache]}
Android:
I/flutter (16944): headers: {connection: [keep-alive], cache-control: [no-cache, no-store, max-age=0, must-revalidate], set-cookie: [var1=122d; Domain=localhost; Expires=Thu, 20-May-2021 17:56:54 GMT; Path=/a/b], date: [Tue, 20 Apr 2021 17:56:54 GMT], vary: [Origin, Access-Control-Request-Method, Access-Control-Request-Headers], x-frame-options: [DENY], pragma: [no-cache], x-xss-protection: [1; mode=block], x-variable: [Bearer eyxxxxx], x-content-type-options: [nosniff], server: [nginx/1.18.0 (Ubuntu)], expires: [0]}

cws-alb avatar Apr 20 '21 19:04 cws-alb

I'm facing the same issue too :(

nelsonesser avatar Apr 26 '21 01:04 nelsonesser

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

stale[bot] avatar Jun 04 '21 15:06 stale[bot]

I just ran into the same issue with http-only cookies not beeing saved. My fix was to first configure my Flask backend to support credentials. My flutter dev instance is running in chrome on port 1234.

app = Flask(__name__)
app.config.from_object(Config)
CORS(app, origins=["http://localhost:1234"], supports_credentials=True)

I then added an interceptor with options.extra["withCredentials"] = true to my dio object:

   dio.interceptors.add(InterceptorsWrapper(onRequest: (options, handler) async {
      options.extra["withCredentials"] = true; 
      return handler.next(options);
   });

I found this solution by looking throught the dio source and found this in browser_adapter.dart:

/// Whether to send credentials such as cookies or authorization headers for /// cross-site requests. /// /// Defaults to false. /// /// You can also override this value in Options.extra['withCredentials'] for each request bool withCredentials = false;

mxkdev avatar Jun 16 '21 16:06 mxkdev

I'm facing the same issue does anybody found a workaround?

anikket786 avatar Jun 22 '21 17:06 anikket786

facing the same problem.

tl-vt avatar Jun 26 '21 18:06 tl-vt

facing the same issue. env:

Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.2.3, on Linux, locale en_US.UTF-8) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) [✓] Chrome - develop for the web


Desc:

if set Credentials =false => response headers just include:content-type: application/json. else set Credentials=true => Flutter throw ERROR: Error: DioError [DioErrorType.response]: XMLHttpRequest error. dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 909:28 get current packages/dio/src/dio_mixin.dart 819:20 assureDioError packages/dio/src/dio_mixin.dart 678:13 _dispatchRequest dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 60:31

superluo avatar Jul 04 '21 08:07 superluo

I got same problem..

probepark avatar Jul 21 '21 09:07 probepark

Same here. Using Dio in Flutter Web, there seems to be no way to receive HTTP headers other than content-length and content-type. This is not an issue when using Dio in Android or IOS.

supermuesli avatar Jul 29 '21 21:07 supermuesli

same issue in web . set cookie can be found in browser net response, but can't get it from code response.headers

sinoory avatar Aug 01 '21 15:08 sinoory

same issue in web . set cookie can be found in browser net response, but can't get it from code response.headers

09094080-Tao-Jiao avatar Aug 17 '21 00:08 09094080-Tao-Jiao

@supermuesli @sinoory, This is more of a browser related issue. The below explanation is all what you need to know,

  • Cookies that do not specify a SameSite attribute will be treated as if they specified SameSite=Lax, i.e. they will be restricted to first-party or same-site contexts by default.

  • Cookies that are intended for third-party or cross-site contexts must specify SameSite=None and Secure.

The header cookies from your server probably doesn't contain SameSite attribute and that's why the header cookies are not accepted by the browser. So to make it work either we have to add SameSite=None attribute in response header at server side or The web app needs to be hosted on the same domain as the server from which response is coming.

anikket786 avatar Aug 17 '21 05:08 anikket786

@anikket786 seems true when run in web. But when run it on android phone , still can not find cookie from response.headers

sinoory avatar Aug 19 '21 11:08 sinoory

I have just faced this issue and after making the same call repeatedly I've noticed that in the response object, sometimes this header is present and sometimes not.

image
Set-Cookie missing
image
Set-Cookie present

Achieved running locally on MacOS

mludovico avatar Sep 07 '21 18:09 mludovico