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?

user776686
  • 7,933
  • 14
  • 71
  • 124
  • 1
    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. – Irv Lennert Nov 21 '17 at 01:45
  • same here, it's also returning null if response is false. – nightElf91 Jan 01 '18 at 05:25

1 Answers1

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://stackoverflow.com/a/46081328/3133446

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

nightElf91
  • 650
  • 1
  • 7
  • 28