Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upHttpClient do not detect header params #20554
Comments
|
Sorry, your problem seems to be a different one, please provide a (valid) reproduction, you can use this template as a starting point: http://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5 |
|
I'll look into this, but logging the headers isn't going to show them correctly, as they're lazily parsed. |
|
I get the lazy parsing, but res.headers.get('Authorization') also doesn't work for me though. |
|
@GAlexMES From the screenshot you're definitely misusing it.
They'll never show up in same response. |
|
As far as I see, it is also not possible to add a Header to a post or get request.
When I have a look using the Firefox Dev Tools, the Authorization Header is not present. Environment
|
|
All CORS headers are added in response, that's not related to Angular, and you're adding them in wrong place. |
|
@trotyl To your second comment: |
|
@MyGitHubTests You did not even set |
|
Where and how should I set them? Why do they show up in browser network tab? |
|
@MyGitHubTests please try #5237 (comment) |
|
@AhmedHasnaoui your solution fix this problem |
|
I'm afraid I'm seeing similar issues after moving to Angular 5.1.1
Both One of the calls that is expected to be valid in this case and start checking for
Response
This seems to be matching the
result: This seemed to work when we were using version Angular 4.1.3, where we received results similar to this when testing this specific service call:
Any ideas @trotyl? We've been careful to examine if we weren't missing any headers to make this work but couldn't spot any. We're afraid we've actually found a regression bug between Angular 4.1.3 and Angular 5.1.1. |
|
It does seem to work on regular HttpClient calls, but not in an HttpInterceptor. We're on Angular 5.1.1. Http call with HttpClient:
HttpInterceptor code (part of it):
Http response from the backend:
Console logging:
(that the request passes through the interceptor twice: once as OPTIONS pre-flight and once as GET) The logging from the HttpClient call includes Content-Type and our custom response headers. Giving it a bit more thought, I'm wondering. Do we have access to both the http request and http response in an interceptor? We might be looking at the request instead of the response ;-) |
|
@Bjeaurn @bertjan HttpClient & Interceptor came in 4.3.0, if it's an HttpInterceptor specific issue, didn't get how could it worked in 4.1.3?
XHR will not expose the |
|
OK, found the problem. Caused by ourselves, of course ;-) We were inspecting the REQUEST headers while we should instead have been looking at RESPONSE headers. This works (in the intercept(..) method):
The reason for our confusion is probably that (from our point of view) the signature of intercept(..) changed a bit. So previously we were only working with an http response in our interceptor, now we both have the http request and the 'next' chain. |
|
Our mistake, I'm sure you've cleared up most of the confusion in this issue already. It works if you set the correct headers from your service and plug into the RESPONSE for both the HttpInterceptor and the HttpClient. |
|
@bertjan Would you mind posting your map function please from your comment? I am having the same issue and am not familiar with the pipe function. |
|
@mjohns39 sure:
Function |
|
Nevermind. I found it's a function in the rxjs library. For those confused like me: import {map} from 'rxjs/operators' Unfortunately, this didn't solve the problem for me. Stepping through, I am eventually able to see the Json body of the response from my rest api in the map method of the interceptor. I am also able to see the Access-Control-Expose-Headers set in the network tab of Chrome dev tools. But back in the map method of the interceptor, none of the Http headers are set. |
|
(apologies in advance for formatting as I am on mobile) The map method of the response interceptor : import {map} from 'rxjs/operators; *Note that you still have to set the Access-Control-Expose-Headers header explicitly on the backend. |
|
How is it going ? I spent my day for this bug :( |
|
The main thing we did wrong was we didn't put the |
|
@duylampersol Most people on the thread have found it's due to their own faults, and there's still no reproduction from the remainings, if you can provide a repro, it'll be "solved" quicker. |
|
@trotyl @duylampersol I'm not sure if there's even a bug. The big difficulty with finding the fault here (on the dev's own side) is that there's a part of Server settings that is very super irrelevant for the On the |
|
Still having an issue with this even with the response headers showing. I am unable to use the headers appearing in console or pass them along. |
|
For those this could help. Another way of doing this (when using expressjs) is to set the cors as such I prefer the first method as this gives me choice when that header is allowed (and used). |
|
Nice @efikatec, I think the main point in regards to Angular is that you need to set these headers on the Server side, not within Angular. So I'm not sure how relevant your specific example is, but it's a good idea to show what to look for in your environment. |
Solved my problem, i've added both headers in the "OPTIONS" and "POST" responses, angular have managed to read all of then I'm using Spring boot, in case someone wants to know, you just need to filter the requests by its methods, in my case, this is my method which generates a JWT token and passes it to the response's header The OPTIONS request (this method filters all authorized requests using Spring Security package):
The POST request:
|
|
Can we close this issue, I'm quite sure there is no issue anymore. You can parse headers as of now and the main issues people keep talking about are @JayendharPrakash Can you close this as resolved please? |
|
Fixed by specifying |



I'm submitting a...
Current behavior
HttpClient do not detect the custom header params that are sent from the API. While using Http we were able to access it and make use of it.
Expected behavior
HttpClient should detect the custom headers and make them accessible by the code. We tried with both Http & HttpClient. I works fine with Http but not with HttpClient.
Minimal reproduction of the problem with instructions
We used both Http & HttpClient to fetch the header params. Http returns the header, but HttpClient doesn't.
When checked in console, http returns the response with headers but httpClient returns an empty array.
When checked in networks tab of browser inspector, it displays all the header parameters that are sent from API.
The server has the following CORS options:
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders: 'Origin,X-Requested-With,x-access-token,Content-Type,Authorization,Accept,tokenkey',
exposedHeaders: 'Content-Type,Authorization,tokenkey'
What is the motivation / use case for changing the behavior?
I want to access the token key that is sent from the API and store it to authorize and validate the user. I am unable to get the tokenkey from the API.
Environment