MightGod
MightGod

Reputation: 474

Angular 8 get "Set-Cookie" header from HttpResponse

I have the following Java Spring REST API method:

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity login(@RequestBody LoginRequest request) {
    request.getSession(true);
    LoginResponse res = this.authService.login(request);
    return new ResponseEntity<>(res, HttpStatus.OK);
}

When called using Postman or from a FireFox browser, I can clearly see the "Set-Cookie" header:

enter image description here

Yet, when I'm using console.log to print the response in Angular, I don't see this header:

enter image description here

This is the REST call in Angular:

this.http.post<Login>(this.url, this.loginRequest, {
  headers: AuthService.getHeaders(),
  observe: 'response',
  withCredentials: true
}).subscribe(
  response => {
    console.log(response);
});

I did add withCredentials: true and observe: 'response' to the request call as suggested, and I do get the whole response, but without the cookie. What I want to do is to get a cookie after a successful login, which will be delivered with any request afterwards for authentication in the server.

Thanks for the help!

Upvotes: 4

Views: 11181

Answers (1)

MightGod
MightGod

Reputation: 474

Seems that browsers do not share the cookies to the client if it's not https. I had to follow this guide in order to serve the application in https mode, and then I was able to see the cookie in the server side.

Upvotes: 4

Related Questions