question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty headers in HTTP Response with Angular 4.3's HttpInterceptor

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

This is how I send my HTTP request: return this.http.get(url, { observe: 'response' })

I would like to read the HTTP headers of a HttpResponse in my HttpInterceptor:

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(request)
            .do(event => {
                if (event instanceof HttpResponse) {
                    this.logger.logDebug(event); // Headers are missing here
                }
            })
            .catch((err: HttpErrorResponse) => {
            // Do stuff
    }
}

The event seems to have no headers, and even in the Chrome Dev Console I cannot see any headers:

headers

However, when using Postman, I can see the headers in the response (as expected)

Connection →keep-alive Content-Length →14766 Content-Type →application/json Date →Fri, 04 Aug 2017 14:50:46 GMT Server →WildFly/10 X-Powered-By →Undertow/1

The official docs for HTTP says to get the headers like this:

http
  .get<MyJsonData>('/data.json', {observe: 'response'})
  .subscribe(resp => {
    // Here, resp is of type HttpResponse<MyJsonData>.
    // You can inspect its headers:
    console.log(resp.headers.get('X-Custom-Header'));
    // And access the body directly, which is typed as MyJsonData as requested.
    console.log(resp.body.someField);
  });

Expected behavior

I was expecting to see the same headers in Angular as with Postman

Environment


Angular version: 4.3.3

Browser:
- [x] Chrome (desktop) version XX

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
TheParad0Xcommented, Aug 8, 2017

@robwormald You were right. It was a CORS problem. I needed to explicitely expose my custom header. Thank you

2reactions
alxhubcommented, Aug 7, 2017

Can you share the code you’re using to actually read the headers? Logging the HttpHeaders instance is going to be misleading - headers are lazily parsed from the response header string on first access. It looks like your instance has a lazyInit function and is poised to perform this parsing when the headers are first accessed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Intercepting HTTP Response headers with Angular 4.3's ...
I found the answer. It was (of course) a CORS Problem. I am using a CORS Filter and I needed to explicitely expose...
Read more >
Intercepting Http Response Headers With Angular 4.3'S ... - ADocLib
In my Angular 7 project I receive an error [ts] Cannot find module Empty import TypeScript ES6 Some modules do not export any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found