Amnestic
Amnestic

Reputation: 660

Cookie is not set on localhost in chrome or firefox

I am working with a Jersey server which returns a cookie in the following way:

return Response.ok()
    .cookie(
        new NewCookie(
            "userAccessToken", userTokenDTO.getToken(), "/", "", 
            "what is this", 3600, false
        )
    ).build();

When I call the method which returns the cookie, I get the following result in chrome: Request and response headers

I can even see that chrome has recognized my cookie: Cookie recognized

But for some reason it isn't set in the cookie tab:

No cookie shown

I have tried setting the domain both to false, null, "", creating an entry in the hosts file renaming 127.0.0.1.

return Response.ok()
    .cookie(
            new NewCookie(
                    "userAccessToken", userTokenDTO.getToken(), "/", "127.0.0.1",
                    "what is this", 3600, false)
    ).build();

Works in IE 11, but still not Chrome nor Firefox...

I have tried multiple time to insert another host name for 127.0.0.1. In this example it is text.myexample.com. It still doesn't work in any other browser than IE11.

return Response.ok()
    .cookie(
            new NewCookie(
                    "userAccessToken", userTokenDTO.getToken(), "/", "test.myexample.com",
                    "what", 7200, false)
    ).build();

I tried to do the following in the console of Google Chrome:

document.cookie = "userAccessToken=72bebbe0-44fd-45ce-a6e1-accb72201eff;Version=1;Comment=what;Domain=test.myexample.com;Path=/;Max-Age=7200"

Which is the cookie in the header returned by the server in Chrome. That works fine. I have literally no clue what is going on here.

Upvotes: 16

Views: 18393

Answers (2)

Amnestic
Amnestic

Reputation: 660

Turns out the problem was related to the fetch library I am using. If you do not include { credentials: "same-origin" } in the request, the response cookie isn't being set.

For more information, see: https://github.com/github/fetch/issues/386.

Upvotes: 13

gladiator
gladiator

Reputation: 1363

Its a issue with localhost only ,works well on other urls see below link here cookie works but in local it fails http://jerseyexample-ravikant.rhcloud.com/rest/jws/say/Hi

for localhost you can go with below .

return Response.status(200).entity(output)

                .header("Set-Cookie", "userAccessToken=toke;lang=en-US; Path=/; Domain=localhost")
                .build(); 

enter image description here

See the network tab

Response Headers Content-Length:18 Content-Type:text/html Date:Fri, 25 Nov 2016 10:19:15 GMT ProcessingTime:0 millisecs Server:Apache-Coyote/1.1 Set-Cookie:userAccessToken=toke;lang=en-US; Path=/; Domain=localhost

Network tab

Upvotes: 0

Related Questions