How to get value in Response header Angular2 Ionic 2

i want to get session token in response header(Set-Cookie).how can i access values in Response header ?

var headers = new Headers();
            headers.append('Content-Type', 'application/json');
            console.log('url',this.loginUrl)
            this.http.post(this.loginUrl,
                JSON.stringify({ "username": value.username, "password": value.password }),
                { headers: headers })
                .map((res: Response) => 
                    res.json())
                .subscribe((res) => {
                    console.log("res", res);
                    this.loading.hide();
                    if (res.message_code == "SUCCESS") {
                        this.nav.setRoot(HomePage, {
                            username: value.username,
                        });
                    } else {
                        let alert =  Alert.create({
                            title: "Sign In Error !",
                            subTitle: 'Please Check Username or Password.',
                            buttons: ['Ok']
                        });
                        this.nav.present(alert);
                    }
                }, err => {
                    this.loading.hide();
                    console.log('error', err);

                }); 

this is my header response

image

.subscribe((res) => {
res.headers._headersMap.get(‘header-you-want’);
}

hi thanks for answer.but now im getting undefined as value

Could you show the new code?

Hi,

Can someone post working code example on how to read HTTP response headers? I am having issues with this.

Thanks

The res has already the headers data. Just don’t call .map function

Thanks! I figured that out 5 minutes after posting question :smile:

Hi, How are you?
Could you tell me how did you to get Set-Cookie value from the header? I am trying to get it with map, but it isn’t work, What way did you use?

My code:
this.http.post(‘http://localhost:8080/NXV/Login’, body)
.map((response: Response) => {
console.log(response);
});

Thanks!

HI,

Still i can’t able to get the Set-cookie in response header. could you please help how you solve this?

Thanks in advance.

Regards,
Venk

Try this one
this.http.post(APIVariables.API_URL+‘user/login’,body,options)
.map(res => res.json()).subscribe(res => {
// let alert = this.alertCtrl.create({
// title: ‘Response message’,
// subTitle: res.message,
// buttons: [‘Dismiss’]
// });
// alert.present();

this.userData.login(res.data.email);
this.navCtrl.setRoot(HomePage);

}, err => {
this.spinner=false;
let alert = this.alertCtrl.create({
title: ‘Response message’,
subTitle: err,
buttons: [‘Dismiss’]
});
alert.present();
});

Did anyone get the Set-cookie in response header?