Where to set responseType to http GET for HttpClient in angular
10,018
You can specify that the data to be returned is not a JSON using the responseType
. See the Requesting non JSON data
In your example, you should be able to use:
return this.http.get(
`${environment.apiBaseUrl}/blah`, { responseType: 'text' })
EDIT
You can set the responseType as blob,
return this.http.get(`${environment.apiBaseUrl}/blah`, { responseType: 'blob' });
Related videos on Youtube
Author by
Drew13
I'm an associate software developer six months out of college. My main focus in school was Java, but I had some experience building an application using PHP. Now I'm mainly working on building an Angular2 UI.
Updated on June 12, 2022Comments
-
Drew13 about 2 years
I have a service that makes an http call to my backend:
exportSubs(param: Param): Observable<Sub[]> { return this.http.get<Sub[]>( `${environment.apiBaseUrl}/blah`, {headers: this.httpUtil.getReqHeaders}) .catch(error => this.httpUtil.handleError(error)); }
where do I set responseType?
-
mast3rd3mon almost 6 yearsin the headers like normal
-
jonrsharpe almost 6 years@mast3rd3mon that's absolutely not what you normally do! Do you mean in the same object as the headers?
-
jonrsharpe almost 6 yearsDid you read the docs? They show exactly where to set it.
-
mast3rd3mon almost 6 years@jonrsharpe yes it is what you do, its the
Accept
header, unless he means what the angular http response type is, in which case it is already set to an array ofSub
-
jonrsharpe almost 6 years@mast3rd3mon I think you misunderstand the question, I'd suggest also reading the docs I just linked.
-
mast3rd3mon almost 6 yearsi dont think i do misunderstand at all
-
jonrsharpe almost 6 years@mast3rd3mon well you're talking about the
Accept
header and the OP is talking about the AngularHttpClient
'sresponseType
option, so...
-
-
jonrsharpe almost 6 yearsThat's not even valid syntactically, so I doubt you tried it.
-
Drew13 almost 6 yearsthen I get error: "Expected 1-2 arguments, but got 3."
-
mast3rd3mon almost 6 years@Drew13 sounds like youre passing an extra parameter into the call,
get
methods only take 2 paramaters, the url and optional http options -
Drew13 almost 6 years@mast3rd3mon yeah I had to get rid of where I set my headers. I need my responseType to be of type 'blob'. I'll just figure it out
-
Sajeetharan almost 6 yearsyou can set the type as blob
-
Drew13 almost 6 years@Sajeetharan that gives me Argument of type '{reponseType: 'blob;'} is not assignable to parameter type '{ headers?: HttpHeaders | {[header: string]: string | string[]: }; observe?: "body"; params?: Type '"blob"' is not assignable to type '"json"'
-
JB Nizet almost 6 years@Drew13 you need to read the API documentation, and the http guide. If the response type is a blob, there's no way the generic type can be Sub[]: your request is supposed to return a blob, not a JSON array of Subs.
-
Bravo over 2 yearsis there anyway I can set { responseType: 'blob' } and { responseType: 'text ' } together, as my error response is not able to work with blob