Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular HttpClient ResponseTyp Error

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...

like image 468
Klitz Avatar asked Mar 05 '18 20:03

Klitz


People also ask

What is responseType ArrayBuffer?

"arraybuffer" The response is a JavaScript ArrayBuffer containing binary data. "blob" The response is a Blob object containing the binary data.

What type does HttpClient get method return?

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.

What is the CLI command used to create a new angular HttpClient in angular 8?

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.

What is HttpClient post?

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.


2 Answers

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

like image 152
Pankaj Parkar Avatar answered Sep 21 '22 17:09

Pankaj Parkar


For me, it worked with " as 'json' " syntax:

responseType: 'blob' as 'json'
like image 34
Tomáš Páchnik Avatar answered Sep 20 '22 17:09

Tomáš Páchnik