Search code examples
typescriptapihttphttprequesthttpresponse

Get header value from http response in Typescript


I am trying to access a new header I have added to my API response in TypeScript. In the Network tab of my developer tools, I can see that the new header I added is present, but when I try req.headers.get("x-web-frontend-version") I just get null.

Here is my code and image of my network tab:

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    let headers = req.headers;

    console.log('HEADERS: ' + headers.get('x-web-frontend-version'));

    const authReq = req.clone({
      headers: headers
    });

    return next.handle(authReq);
  }
}

Image:

enter image description here


Solution

  • Assuming that the function Intercept is in some service file, such as some.service.ts, in the component file, some.component.ts, I would try the following:

    const xWebFrontednVersion: string;
    this.someService.intercept(req, next).toPromise().then(response => {
            this.xWebFrontednVersion = response.headers.get('x-web-frontend-version');
    })