Reputation: 21
hope you can help me with this.
I am developing an application using Angular 8, that calls some Java REST Services (not a Spring backend, the services are written using only Jersey). When developing backend services, i used Postman app to test them, and this took me to an error: it seems that Postman automatically manages JSESSIONID, so when i try to call my service in Angular application, i realised that i have to do it manually.
The real question is, how to manage that ID in Angular? When i call my first service, a login service, in "Network" session of Chrome console i can see in response's headers the field:
Set-Cookie: JSESSIONID=averylongstring
I tried, following different other questions, to get it with
response.headers.get('Set-Cookie')
but it returns null. If i write
console.log(response.headers)
in .subscribe method, i can't see any relevant header, picture is following:
How can I access to that id? Is an error in Angular response handling or in service? I tried to return first a custom serializable bean and then a javax.ws.rs.core.Response object, hoping to fix, but i get the same situation.
Upvotes: 0
Views: 8488
Reputation: 331
Sometimes, mostly while responding to authentication/authorization request, the server uses "Set-Cookie" to set an "Http-Only" cookie for JSESSIONID to prevent XSS exploits, or so.
In these cases, you cannot read/get JSESSIONID cookie from client side, even if you see it on your browser's console.
Upvotes: 3
Reputation: 2004
We use ngx-cookie-service for managing cookies in our Angular applications. Here is how you can set it up:
npm install ngx-cookie-service --save
or
yarn add ngx-cookie-service
Add the cookie service to your app.module.ts
as a provider:
import { CookieService } from 'ngx-cookie-service';
@NgModule({
...
providers: [ CookieService ],
...
})
export class AppModule { }
Then, import and inject it into a constructor:
constructor( private cookieService: CookieService ) {
this.cookieValue = this.cookieService.get('JSESSIONID');
}
Alternatively you can just access document.cookie
but that would be much less explicit.
Upvotes: 0