Angular HttpClient returns null value for response of zero
Asked Answered
A

1

7

I have migrated my Angular 4 service from old Http to the new HttpClient. A request that is returning plain zero 0 as the response, is returning an observable with null.

The request:

return this.httpClient.get(url, options)
  .do(res => console.log('service returns', res)) as Observable<number>;

will console log null, but when I open that request in devtools > Network, I see that the server responded with 0.

What am I doing wrong?

Arable answered 5/10, 2017 at 7:42 Comment(2)
Your not alone, I'm having almost the same problem... Although with post instead of a get, I get back a null as well, even though the network debugging tools are showing that the backend service is sending the info in the response as expected.Applique
same here, it's also returning null if response is false.Vimen
V
1

HttpClient expects JSON object by default. As, you are responding with just the value, it returns the value as null(no json object!)

Got the answer here https://mcmap.net/q/1071699/-response-body-is-null-status-is-200

Just change the 'responseType' to 'text' when sending the request.

Vimen answered 1/1, 2018 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.