Search code examples
angularangular-httpclient

How to get HttpStatusCode or why StatusCode is undefined


I have an Angular Client and I do a List request like this.

getMyList(body: SearchCriteria): Observable<HttpResponse<MyResult[]>> {
  return this.http.post<HttpResponse<MyResult[]>(url,body);
}

And I execute this with

httpService.getMyList(body).subscribe((response: HttpResponse<MyResult[]>) =>
{
 console.log(response.body)
 console.log(response.status)
 console.log(response.statustext)
 console.log(response)
}
);

Console Output of this is:

undefined
undefined
undefined
[{my list....}]

So my question, why is "response" directly my list and not a HttpResponse. How to get the statuscode?

My expectaion was.

response.body  // is my list result
response.status //is a HttpStatusCode

What i do wrong?


Solution

  • You need to pass options object with the http.post, similar to the following:

    getConfigResponse(): Observable<HttpResponse<Config>> {
      return this.http.get<Config>(
        this.configUrl, { observe: 'response' }
      );
    }