One of the common scenarios where the ability to add headers to angular http requests comes in handy is authentication API calls using tokens. It is very easy to add headers to individual calls like below but it’s a little bit more work to do so for all http requests.
Adding header to single http call
Adding header to all http calls
For this, we have to extend the BaseRequestOptions class.
There are some differences between extending BaseRequestOptions and RequestOptions- should research this someday.
Below code gets some value from a different service and sets it as ‘CustomHeader’. super(); is needed in the constructor for derived classes.
request.service.ts
Now, we have to configure Angular to use the above class instead of the default RequestOptions.