Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get response header in angular

I try to get the headers from a http response if the error code is 401 for the digest auth.

This is my code:

 this.http.post(this.hostname + ':10000/users/auth2', { })
        .subscribe(res => {
            console.log(res);
        },
        error => {
            console.log(error);
        });

Everything work's fine if the post request return a 200 code. If I return a 401 code from the backend I cannot access the headers.

The chrome debugger shows the headers. Postman call also work's fine with digest auth.

like image 342
PMe Avatar asked Dec 07 '25 10:12

PMe


1 Answers

The default option for observe parameter is body, change it by adding {observe: 'response'} to your post method:

 this.http.post(this.hostname + ':10000/users/auth2', {}, {observe: 'response'})
    .subscribe(res => {
        console.log(res);
    },
    error => {
        console.log(error.headers);
    });
like image 156
OST Avatar answered Dec 09 '25 22:12

OST