Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get cookie header #163

Closed
AlexKovalevych opened this issue Jun 15, 2015 · 13 comments
Closed

Unable to get cookie header #163

AlexKovalevych opened this issue Jun 15, 2015 · 13 comments

Comments

@AlexKovalevych
Copy link

The server sends me a response header "SET-COOKIE" and i'm not able to retrieve it - it just doesn't appear among headers. Probably it doesn't pass a normalizer test, is this how it should work or i missed something? How can i retrieve it? Thanks in advance.

@mislav
Copy link
Contributor

mislav commented Jun 15, 2015

If the cookies apply to the current domain/page, then you should be able to access them via document.cookie

@dgraham dgraham closed this as completed Jun 15, 2015
@AlexKovalevych
Copy link
Author

Cookie is not available in the document.cookie, otherwise i wouldn't create an issue here. The header "SET-COOKIE" completely ignored and not available in the response.headers.

@dgraham dgraham reopened this Jun 15, 2015
@dgraham
Copy link
Contributor

dgraham commented Jun 15, 2015

Does the header appear if you instrument the header parsing with console.log?

@dgraham
Copy link
Contributor

dgraham commented Jun 15, 2015

I think this is a duplicate of #138.

@mislav
Copy link
Contributor

mislav commented Jun 15, 2015

Yes, I don't think XHR is able to access set-cookie response header. Most likely native fetch() follows this as well.

@AlexKovalevych
Copy link
Author

Correct, so what should be a solution here? Send a different custom header or you know a better approach?

@mislav
Copy link
Contributor

mislav commented Jun 15, 2015

Well, we don't know what your problem is, so we can't offer a solution yet. The browser doesn't want you to read set-cookie yourself from an ajax response. It wants to handle the cookies natively, as it does. We don't know what you want to achieve with said cookies.

@dgraham
Copy link
Contributor

dgraham commented Jun 15, 2015

Maybe you're running into the same-origin issue from #142? Cookies are handled transparently with requests and responses. There shouldn't be a need to read them out of the response headers.

@AlexKovalevych
Copy link
Author

Ok, i see, the cookie is needed to authenticate the user - thats how it works by default. Server set the cookie and i send it back with each request (pretty common approach). The issue i faced with - is that cookie is not set for some reason, probably i used fetch library wrong. Thank you for your response, i'll investigate.

@mislav
Copy link
Contributor

mislav commented Jun 15, 2015

@AlexKovalevych I understand. Then, you should leave it to the browser to handle all the cookie stuff. However, if the browser is not sending the authentication cookie back to the server when you're using fetch(), that's actually expected (and per-spec) with default invocations of fetch.

To have fetch send cookies back to your server, and preserve authentication information, you will need to pass the option to it every time:

fetch(url, {credentials: 'same-origin'})

@fanqidaoerxing
Copy link

i have meet the same problem, but finally i realize that i don't need to get the 'Set-Cookie' from headers, use credentials: 'same-origin' (or 'include'), it helps me to send my browser cookie which server sends to me back to them. whatever they send to us, like login state or other values, its totally controlled by the server.

@MartinDawson
Copy link

@mislav Thanks a lot. Was so hard to find this answer.

@jobyktom
Copy link

Thank you very much @fanqidaoerxing. include option has done it for me.
fetch(_url, {credentials: 'include'}).then( ....

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants