When I want to append headers to a standard get, post, put or delete request, I can easily pass it inside the options object:
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'my-auth-token'
})
};
this.httpClient.get<any>(
'http://myapi.com/endpoint',
httpOptions
)
However, when I am using the request<R>(req: HttpRequest<any>): Observable<HttpEvent<R>>; method of HttpClient, I am unable to pass any headers in anywhere as it doesn't accept any other parameters. I need to make my request this way since I need to append a file object to the body. This is the way I make the call:
this.httpClient.request(
new HttpRequest(
'POST',
'http://myapi.com/endpoint',
formData, // contains file object
)
);
Is there a way to work this around?
HttpRequestobject itself angular.io/api/common/http/HttpRequest