Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular HttpClient returns null value for response of zero

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?

like image 323
user776686 Avatar asked Oct 05 '17 07:10

user776686


1 Answers

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.

like image 114
nightElf91 Avatar answered Nov 02 '22 10:11

nightElf91