3
votes

I'm not getting the actual http headers when making the http post call using Angular 5 common httpClient. I'm passing observe: 'response' in request to get the full response (by default httpClient returns only the body in response).

I want to read the Location value from http header response. I can see that Location is available in response headers using chrome browser network view.

But the same is not available when I read the httpClient response in Angular.

screenshot - console.log:

enter image description here

import {
    Component,
    OnInit
} from '@angular/core';
import {
    HttpClient,
    HttpHeaders,
    HttpResponse
} from '@angular/common/http';

getData() {
    const headers = new HttpHeaders({
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json'
    });

    const url = 'http://localhost:1003/jaxrs/report/3003/params/';

    this.http.post(url, {}, {
            headers: headers,
            observe: 'response'
        })
        .subscribe(res => {
            console.log(res);
        });
}

screenshot - network view

enter image description here

Can someone please help me with this? Thanks in advance.

1
Look at the below link, stackoverflow.com/a/42106435/337128 which might enlighten you on why you are not able to access it. From the server side i guess you need to expose location to access it. w3.org/TR/cors/#access-control-expose-headers-response-headerThalaivar

1 Answers

4
votes

You have to observe the response for that: like below example:

this.http.post(yourURL, null, { observe: 'response' })
.subscribe(res => {
    console.log(res.headers);
})

Hope this answers your question.