0

I have an angular page and is being rendered through a component - say DashboardComponent. A third party site redirects to my angular page and set some information, like userId, in the header(not in the url parameter).

Now I would like to know that how will I fetch this header information in my angular component (or in angular project also will do if there is a proper way to do so.)?

My thought goes to might be we can get it from index.html, but its being an html file, I am not sure if this is possible.

Please help in this.

  • 1
    Possible duplicate of [How do I access the HTTP request header fields via JavaScript?](https://stackoverflow.com/questions/220149/how-do-i-access-the-http-request-header-fields-via-javascript) – jonrsharpe Jan 03 '19 at 09:23

1 Answers1

0

If you had an http service file you could use something like this:

this.httpService.getList()
.subscribe((res) => {

                console.log(res.headers.get('x-paging-pagecount'));

            });

This for example would get the value of "x-paging-pagecount" from the headers.

Andrew Howard
  • 2,818
  • 5
  • 33
  • 59
  • Its a third party website, redirecting to my angular site. So how can a third party site call service defined at my end. I need to fetch data from headers set by third party site when they are redirecting to a url on my angular page. – Ritesh Bathwal Jan 03 '19 at 09:50
  • In short a third party site can't call your service, because all the code gets minified and you won't know what it is called. Your third party site will just have to know the api line that it wants to call. – Andrew Howard Jan 03 '19 at 10:35
  • Hi Andrew. The scenario is that third party site will be sending request to my angular page route (by redirecting to my angular route) and in this requst it will be setting up the header. So I would like to know is it possible to fetch header information from angular end for this request? If yes, then how? If no then why? – Ritesh Bathwal Jan 03 '19 at 11:16
  • In that scenerio you could set a queryparam from the third party site to go to your angular route, for example "http://www.example.com?thirdparty=true" and then you just put some if statement logic to get the headers. You could also use this approach to have 2 different api calls if they need different headers being set. – Andrew Howard Jan 03 '19 at 11:45
  • Cant add the information in the query params as it is sensitive info. (Its user access token). So that will compromise the security. Any other solution? – Ritesh Bathwal Jan 04 '19 at 06:30