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?

2
  • 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. Commented Nov 21, 2017 at 1:45
  • same here, it's also returning null if response is false. Commented Jan 1, 2018 at 5:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.