Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't read custom header in HttpResponse from HttpClient in a Ionic Angular application

Tags:

I'm requesting an endpoint that creates a new resource and returns a 201 response containing a "Location" header with the newly created resource:

Network inspecting on Chrome

However, when I try to get header value as described in Angular guide, I get a "null" value instead of the actual value. Here is my code (note: I'm setting responseType:'text' to avoid Json parsing error as the response body is empty):

(...)
import { HttpClient } from '@angular/common/http';

(...)

  constructor(public navCtrl: NavController, public navParams: NavParams, public http: HttpClient) {
  }

  ionViewDidLoad() {
    let obj = {nome : "SomeName"}
    this.http.post("http://localhost:8080/categorias", obj, {observe: 'response', responseType: 'text'})
    .subscribe(
        (resp) => {
          console.log(resp.headers.get('Location'));
        }
      );
  }

I even tried console.log(resp.headers) in order to check this object, and it shows a completely different structure:

enter image description here

How can I get a custom header from a HttpResponse object from the new HttpClient Angular API?