Let's start with instructions to Proxy to the Backend with proxy.conf.json
. It is normally created at the root of the Angular project structure.
// proxy.config.json sample
{
"/api/": {
"target": {
"host": "localhost",
"protocol": "http:",
"port": 3000
},
"secure": false,
"changeOrigin": true,
"logLevel": "info"
}
}
then when you're using Angular/CLI you can invoke it by:
ng serve --proxy-config proxy.conf.json
Ryan Chenkie has a tech blog on Interceptors for Angular 5, but you can create headers
using HttpHeaders
in your HttpService
:
headers = new HttpHeaders().set('Content-Type', 'application/json');
OR
token = `Bearer ${localStorage.getItem('access_token')}`;
headers = new HttpHeaders()
.set('Authorization', this.token)
.set('Content-Type', 'application/json')
.set('X-CustomHeader', 'custom header value')
.set('X-CustomHeader2', 'custom header2 value')
;
and in your HTTP Request using HttpClient
, add headers to the headers
object option like so using RxJS do
to peak into the data stream:
this._httpClient.post('url/to/api', { headers: this.headers })
.do(data => console.log(data))
;
Or access directly in your component with the below:
this._httpClient.post('url/to/api', { headers: this.headers })
.subscribe(data => console.log(data));