'How to access Response cookies in angular2?

I'm trying to access this cookies (the response ones): enter image description here

When I open the request in the chrome debug tools in the network section I can clearly see that the cookies are present, but how can I access those values from my code? I've never worked with cookies before and I don't know what to do to "extract" them... I'm working on a Ionic2 project using Http.

I've read that the allowCredentials: true header has to be sent but that didn't work...

Here's the request/response details:

enter image description here

Here's the service:

public callLogin(service_guid: string, pos_guid: string, login_data: Object) {

        return this.http.post(
            this.url + service_guid + "/" + pos_guid + "/ack", 
            login_data,
            {withCredentials: true}
            )
            .map(response => response.headers);
    }

And the caller:

this.__posService.callLogin(login_data.service_guid, login_data.pos_guid, {"password": data.password})
                            .subscribe(
                                res => {
                                    console.log("Success:");
                                    console.log(res.get("apsession"); // this returns undefined
                                },
                                err => {
                                    console.log("Error:");
                                }
                            );

When I try to access the cookie from the header it returns undefined. What am I doing wrong here?



Solution 1:[1]

The name of the response header you are trying to get is actually Set-Cookie not apsession. So if you did something like res.get("set-cookie") it would return the first header that matched that name. Since you have more than 1, you could do:

let headers: Headers = res.headers;
headers.getAll('set-cookie');

which returns a list of all headers with that name. You could find apsession in there probably.

See:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nicole Sands