Mace
Mace

Reputation: 1089

Set-Cookie header is not set

I want to receive a response from my server with a Set-Cookie header. I'm using Jersey, spring-boot and Spring security on the server side and angular 2 on the client side. I receive the response, but the Set-Cookie header is not set. It's only in the metadata:

{"statusType":"OK","entity":null,"entityType":null,"metadata":{"Set-Cookie":[{"name":"SomeName","value":"someId","version":1,"path":null,"domain":null,"comment":null,"maxAge":-1,"secure":false}]},"status":200}

But there is no "Set-Cookie" Header: Header

That's the code from the response:

return Response.ok().cookie(new NewCookie("SomeName", "someId")).build();

The method ".header(..)" isn't working as well.

Upvotes: 3

Views: 10416

Answers (2)

stsatlantis
stsatlantis

Reputation: 555

Another alternative:

public ResponseEntity<?> resourceFunction() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add(HttpHeaders.SET_COOKIE, "SomeName=someId");
    return new ResponseEntity(httpHeaders, HttpStatus.OK);
}

Upvotes: 3

aBnormaLz
aBnormaLz

Reputation: 847

Try it like this

public ResponseEntity<?> resourceFunction(HttpServletResponse response) {
    response.addCookie(new Cookie("SomeName", "someId"));
    return ResponseEntity.ok().build();
}

Snippet from Chrome DevTool

ResponseEntity, HttpServletResponse and Cookie are imported from javax.servlet.http.*

Upvotes: 2

Related Questions