title: Angular 2 4 HttpClient. POST request - how to get status code
body: <p>How can I get status code of response POST request?
  My service method return:</p>
  
  <pre><code>return this.httpClient.post(url, body, {observe: 'response'})
              .catch(HandleError.handleErrorClient);
  </code></pre>
  
  <p>In my component I call this method and subscribe:</p>
  
  <pre><code>    .subscribe(
          res =&gt; {
              console.log(res);
              console.log(JSON.stringify(res));
              this.responseStatus = res.status;
              console.log(this.responseStatus);
          },
          err =&gt; {
              this.responseStatus = err.status;
          });
  </code></pre>
  
  <p>But in browser console there is nothing - but when the request contains a error then <code>this.responseStatus = err.status</code> return correct status code, but <code>res.status</code> or <code>res</code> doesn't contain status code, only response body.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Component:</p>
  
  <pre><code>responseStatus: number;
  
  loginUser() {
      this.loginData = this.loginForm.getRawValue();
      this.userLoginService.loginUser(this.loginData)
          .subscribe(
              res =&gt; {
                  console.log('bla bla');
                  this.responseStatus = res.status;
              },
              err =&gt; {
                  console.log(err.status); //401
                  console.log(err.error.error); //undefined
                  console.log(JSON.parse(err.error).error); //unauthorized
                  this.responseStatus = err.status;
              });
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>import {Http} from "@angular/http";
  import {HttpClient} from '@angular/common/http';
  
  ...
  
      constructor(private http: Http, private httpClient: HttpClient) {
      }
  
  
      loginUser(loginData: LoginForm) {
          return this.httpClient.post(this.loginURL, loginData, {observe: 'response'})
              .catch(HandleError.handleError);
      }
  </code></pre>
  
  <p>handle-error.ts</p>
  
  <pre><code>export class HandleError {
  
      static handleError(error: Response | any) {
          let errMsg: string;
          if (error instanceof Response) {
              const body = error.json() || '';
              const err = body.error || JSON.stringify(body);
              errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
          } else {
              errMsg = error.message ? error.message : error.toString();
          }
          console.error(errMsg);
          return Observable.throw(error);
      }
  }
  </code></pre>
  
  <p>POST: /api/login
  <a href="https://i.stack.imgur.com/vun5E.png" rel="noreferrer"><img src="https://i.stack.imgur.com/vun5E.png" alt="enter image description here"></a>
  I'm login correctly, but nothing more... I expected in browser console: <code>console.log('bla bla');</code>. With <code>Http</code> there isn't problems.</p>