PMe
PMe

Reputation: 565

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.

Upvotes: 3

Views: 1954

Answers (2)

Amit Baranes
Amit Baranes

Reputation: 8152

I had a very similar issue, i wanted to get the status code from the response.

Ended up do something like that :

   return await this.http
        .post<any>(this.hostname + ':10000/users/auth2', {})
        .toPromise()
        .then(res => res.data, (err:HttpErrorResponse) => err.headers);

Upvotes: 0

OST
OST

Reputation: 368

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);
    });

Upvotes: 2

Related Questions