How to get HttpClient status code in Angular 4

In the Http module, I can easily get the response code using response.status, but when I used the HttpClient module, I cannot get response.status, it shows undefined.

So how can I get response.status using the HttpClient module in Angular 4. Please help.

+4
source share
2 answers

From this section , this is the corrected code.

http
  .post<T>('/yoururl', {observe: 'response'})
  .subscribe(resp => {
     console.log(resp);
  });

response {observe: 'response'}. then you can view the registered registered and select / select what you want.

+11
source

Try this code for HttpClient:

const req = this.http.post('http://jsonplaceholder.typicode.com/posts', {
      title: 'foo',
      body: 'bar',
      userId: 1
    })
      .subscribe(
        res => {
          console.log(res);
        },
        err => {
          console.log("Error occured");
        }
      );
-1
source

Source: https://habr.com/ru/post/1687131/


All Articles