Situation: I'm facing a challenge in downloading a binary file from a backend system that requires certain data to be posted as JSON-body. The goal is to save this file using File-Saver with the filename specified by the backend in the content-disposition header. In order to access the necessary headers, I believe I need the HttpResponse object.
However, I've encountered difficulties while trying to utilize Angular's
HttpClient.post<T>(...): Observable<HttpResponse<T>>;
method when dealing with a Blob type.
Whenever I make the call:
this.httpclient.post<Blob>('MyBackendUrl',
params,
{observe: 'response', responseType: 'blob'});
the compiler raises an error related to the 'blob' argument ('json' is accepted without any issues):
error TS2345: Argument of type '{ observe: "response"; responseType: "blob"; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'.
Types of property 'observe' are incompatible.
Type '"response"' is not assignable to type '"body"'.
When I tried wrapping the options in a separate object following advice on (without using "as" ...) it resulted in the post(...):Observable being executed but impeded my ability to retrieve the headers.
In addition, even a basic example like
return this.http.get<Blob>('backendUrl', {responseType: 'blob'});
as shown in did not work for me.
Software versions currently in use:
- Angular Version: 5.0.3 (scheduled to update to the latest version 5 in approximately a week)
- Typescript: 2.4.2
- Webpack: 3.8.1