Published 2020-06-18
In the previous post, we already discussed how to make APIs call in Angular.
Today's post was inspired by one of my co-workers when he approached and ask about how to add headers to a POST
request in Angular. Thus, I think this is a good opportunity for me to document this down.
We could leverage HttpHeaders
in Angular to do this.
In the below example,
Authorization
key. httpHeaders
into the headers key of the 3rd parameter of post
function.// Step 1
const httpHeaders: HttpHeaders = new HttpHeaders({
Authorization: 'Bearer JWT-token',
});
// Step 2
this.http.post(url, body, { headers: httpHeaders });
Thanks