You can specify that the data to be returned is not JSON using responseType
.
In your example, you can use a responseType
string value of text
, like this:
return this.http.post(
'http://10.0.1.19/login',
{email, password},
{responseType: 'text'})
The full list of options for responseType
is:
json
(the default)text
arraybuffer
blob
See the docs for more information.
if you have options
return this.http.post(`${this.endpoint}/account/login`,payload, { ...options, responseType: 'text' })
Even adding responseType, I dealt with it for days with no success. Finally I got it. Make sure that in your backend script you don't define header as -("Content-Type: application/json);
Becuase if you turn it to text but backend asks for json, it will return an error...
I just want to add that you have to omit the generic argument on the get/post method method (.get<T>
).
✔️ This will work:
this.http.get(`https://myapi.com/health`, {responseType: 'text'})
❌ This will not work:
this.http.get<string>(`https://myapi.com/health`, {responseType: 'text'})
The later will produce an error:
The expected type comes from property 'responseType' which is declared here on type '{ headers?: HttpHeaders | { [header: string]: string | string[]; } | undefined; observe: "events"; context?: HttpContext | undefined; params?: HttpParams | { ...; } | undefined; reportProgress?: boolean | undefined; responseType?: "json" | undefined; withCredentials?: boolean | undefined; }'
I had the same problem and the cause was That at time of returning a string in your backend (spring) you might be returning as return "spring used"; But this isn't parsed right according to spring. Instead use return "\" spring used \""; -Peace out
You should also check you JSON (not in DevTools, but on a backend). Angular HttpClient having a hard time parsing JSON with \0
characters and DevTools will ignore then, so it's quite hard to spot in Chrome.
Based on this article
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