Cannot read set-cookie from response header in Flutter Web
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.
I'm facing the same issue.
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?
我也遇到了同样的问题
Having the same problem any update?
I'm facing the same issue.
Facing the same issue, any solution?
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]}
I'm facing the same issue too :(
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.
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;
I'm facing the same issue does anybody found a workaround?
facing the same problem.
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
I got same problem..
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.
same issue in web . set cookie can be found in browser net response, but can't get it from code response.headers
same issue in web . set cookie can be found in browser net response, but can't get it from code response.headers
@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 seems true when run in web. But when run it on android phone , still can not find cookie from response.headers
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.
![]() |
|---|
| Set-Cookie missing |
![]() |
|---|
| Set-Cookie present |
Achieved running locally on MacOS

