Repository navigation
Open
Labels
area: common/httpIssues related to HTTP and HTTP ClientIssues related to HTTP and HTTP ClientfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationFeature request for which voting has completed and the request is now under consideration
Milestone
Description
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Response type cannot be set for HttpClient methods.
const options = {headers: headers, params: params, responseType: 'text'};
return this.http.get(url, options).share();
Would show an error
Types of property 'responseType' are incompatible.
Type 'string' is not assignable to type '"json"'.
Expected behavior
It is expected that the response type should be exported like
export type ResponseType = 'arraybuffer' | 'blob' | 'json' | 'text';;
And one would be able to set it using this type. Otherwise the type cannot be changed.
Environment
Angular version: 4.1.1 and still there in 5.0.0-beta.2
as seen here: https://github.com/angular/angular/blob/master/packages/common/http/src/client.ts
Reacted by Lorenzo D'Ianni, harshes, Will, Christoffer Skogh, Mila Rodriguez, Jon Gunter, Michael Sanford, Will, Matthieu Lemonnier, AndreasSchmid1 and 115 more
Metadata
Metadata
Assignees
Labels
area: common/httpIssues related to HTTP and HTTP ClientIssues related to HTTP and HTTP ClientfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationFeature request for which voting has completed and the request is now under consideration
Activity
zaiddabaeen commentedon Aug 8, 2017
A workaround:
alxhub commentedon Aug 9, 2017
@zaiddabaeen currently, this is by design. Typescript needs to be able to infer the
observeandresponseTypevalues statically, in order to choose the correct return type forget(). If you pass in an improperly typedoptionsobject, it can't infer the right return type.Another workaround is:
zaiddabaeen commentedon Aug 10, 2017
I understand, but I believe that's unintuitive and confusing to the developers. I don't recall I ever casted a string to a 'string' before. Enumerating and using types as suggested would sound to me as a cleaner solution.
alxhub commentedon Aug 17, 2017
@zaiddabaeen the problem is for:
What is the type of
res? It depends on the values inoptions- but Typescript has no way to know what those values are if it's not inlined.In other words:
is not equivalent to
In the first one Typescript can infer that the type of
resisObservable<string>, in the second one it cannot be determined via type inference. If we added this feature, we would have to return anObservable<any>which would be a poor experience.I expect most cases where this is desired can be solved with the spread operator:
This way Typescript can infer the return type based on the signature and the value of
responseType, but options can also be passed in without reconstructing the whole object.chrillewoodz commentedon Sep 6, 2017
So instead we have to do workarounds to get the desired effect? That can't be the way to go about this. I've been screaming at my computer for quite a while over this now, I've got a service which wraps the
HttpClientbut trying to setresponseTypedoesn't work, the only way that I can get the error to go away is by doingresponseType: 'text' as 'json'.. None of the workarounds above works.zaiddabaeen commentedon Sep 6, 2017
@chrillewoodz The default is JSON however. Why are you casting it to
json?My approach works, and I can confirm that it is now run on production.
chrillewoodz commentedon Sep 6, 2017
@zaiddabaeen I'm not casting anything to json (as far as I'm aware). This is what I have:
Attempting to add
responseType: 'text'to thereturnofgetRequestOptionsis what throws the error.zaiddabaeen commentedon Sep 6, 2017
Use
responseType: 'text' as 'text'chrillewoodz commentedon Sep 6, 2017
I have already tried all of the above :|
roddy commentedon Sep 20, 2017
I also had this problem, but by removing the
<T>from the get call I was able to get it to work usingresponseType: 'text'as 'text'.Eg this does not work and returns the error:
But this does work:
yusijs commentedon Sep 22, 2017
The only way I got it to work without error was using @roddy's example, and that's with inlined options...
This does not work:

and neither will:

Angular v4.4.3
reppners commentedon Sep 27, 2017
The generic MUST not be used when responseType is specified to something other than
jsonbecausetypeof Twill then be inferred automatically.Take a look at how it is defined for the
get()methodThis makes perfect sense since the type can only be something angular does not know statically when the responseType is
json- all other cases there is no need for a generic.a-kolybelnikov commentedon Oct 10, 2017
@reppners Hi Stefan, thank you for the explanation above. However, no matter how I try I cannot make the new HttpClient to work in a service that I am using to intercept image urls in my app:
Neither
responseType: 'blob'set directly in the GET request, nor setting it in theoptions: { }as mentioned above works.btory commentedon Oct 13, 2017
@a-kolybelnikov You can use something like this in your case:
.get(url, { headers: this.getHeaders(), responseType: ResponseContentType.Blob as 'blob' })60 remaining items
sanioooook commentedon Nov 10, 2020
One of the solutions that helped me get rid of the compiler error(
Type '"text"' is not assignable to type '"json"'.) was creating anHttpRequestusing the constructor. And usinghttp.request. I hope someone will helpLonli-Lokli commentedon Feb 6, 2021
@alxhub does it make sense to reiterate this issue with Typescript 4.1?
evil-shrike commentedon Mar 25, 2021
In 2021 the API is still very weird:
Works:
Does not work (no matches):
have to use responseType: 'blob' as 'json',
sk4xd commentedon Jan 26, 2022
This actually helped me.
noorsaifi commentedon Feb 19, 2022
Worked for me as suggested by @sk4xd .
download(filters: any) {const httpOptions: Object = {responseType: 'blob'};return this.httpClient.post<HttpResponse<Blob>>('/download', filters, httpOptions)}simonthum commentedon Mar 31, 2022
Just my 2 cents:
Please make this a documentation ticket. Everybody is already confused. I just discussed this issue with colleagues.
get() has 15 overloads, 3 of which are generic.
request() has 17 overloads, 4 of which are generic.
I am yet to see a compiler that generates useful error messages in face of so many overloads.
The issue is that both
responseTypeandobserveare often required for proper resolution. This is hinted at in the docs of request(), but not get() or any other HTTP method. But even in request() docs are not really explicit about the requirement to select the right overload. Javascript devs typically do not pay much attention to overloading, and that's a big part of the user base.Maybe I'm misreading this, but in here the observe is optional. But in many cases typescript needs the information for overload resolution.
I think the design intends to keep people on the success path in terms of useful types. I wouldn't "fix" that.
Just make it abundantly clear that the caller has to select the right overload, how to do so, and what to avoid.
gustavostuff commentedon Mar 1, 2023
I tried the workaround mentioned almost at the top (from @alxhub ) but didn't work.
This is a very simple example.
EDIT: We're talking about this headers, right? If so, maybe my params object is not the right type.
EDIT 2: This is what the editor tells me about those 2:
EDIT 3: SO never mind, I think I was confused about usage. This worked:
Lonli-Lokli commentedon Mar 1, 2023
@alxhub I think that with Typescript 5.0 this issue can be finally resolved
https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-rc/#const-type-parameters
l-Legacy-l commentedon Jul 5, 2023
I was getting the same issue using a body and only this method and @rds-rafael one was working for me
Mine is:
const httpOptions: Object = { responseType: 'text' };return this.http.post<string>(this.backendUrl + '/url', body, { params, ...httpOptions })Lonli-Lokli commentedon Jul 5, 2023
@l-Legacy-l
Use could remove
stringtype fromthis.http.post<string>(...)call to make it work.See this example.
For everyone - You should just carefully select the proper overload for your needs.
JeanMeche commentedon May 15, 2024
Since TS 3.4 it is possible to use a const assertion.
gustavoghcmelo commentedon Feb 4, 2025
@sk4xd thanks. It works for me.