I try to download an image(png) from a api. My problem is, that for some reason only json files are accepted by ionic/angular.
return this.http.get(this.authKeys.serverURL, {headers: this.header, responseType: ResponseContentType.Blob});
Following error occurs:
Argument of type '{ headers: any; responseType: ResponseContentType.Blob; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'responseType' are incompatible. Type 'ResponseContentType.Blob' is not assignable to type '"json"'.
For some reason only a response of type json is allowed...
"arraybuffer" The response is a JavaScript ArrayBuffer containing binary data. "blob" The response is a Blob object containing the binary data.
Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.
Run the below command to generate an Angular service, ExpenseService. Open ExpenseEntryService (src/app/expense-entry. service. ts) and import ExpenseEntry, throwError and catchError from rxjs library and import HttpClient, HttpHeaders and HttpErrorResponse from @angular/common/http package.
HttpClient. post() method is an asynchronous method that performs an HTTP post request in Angular applications and returns an Observable. HttpClient. post() has a type parameter similar to the HttpClient. get() request, through which we can specify the expected type of the data from the server.
responseType
holds string value. So you should be passing either of these values
'arraybuffer' | 'blob' | 'json' | 'text';
In earlier version Angular 2, it API was designed to expect enum value for responseType
, check here
For me, it worked with " as 'json' " syntax:
responseType: 'blob' as 'json'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With