Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the status from response in angular http?

I am facing some problem in reading the status code from response.

I am calling api in service,

return this.http.get<string>( this.remoteServer + '/google.com/open', httpOptions);

In my controller I have,

open() {
    this.openService.open(this.selected.qrCode)
    .subscribe(
      data => {
       console.log(data);
       this.toastService.showSuccess('Unlock Successfull', 'success');
      }
    );
  }

Now I want to read the http statustext,

the sample http response I am getting from the above call is.

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "https://google.com/open", ok: false, …}

How to read the status text in the controller.

Please help me

like image 955
Anil Avatar asked Jan 02 '23 05:01

Anil


1 Answers

You can specify { observe: 'response' } as the second parameter to get request which gives you the full response data.

If you want to send other options like headers or params along with it, just rollup all of them in a single object this way.

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json'}), params : myParams, observe: 'response'}

Reference

return this.http.get<string>(
    this.remoteServer + '/google.com/open', httpOptions).subscribe((data) => {
        console.log(data.status);     // staus 200
        console.log(data.statusText);  // OK
});
like image 121
Amit Chigadani Avatar answered Jan 31 '23 18:01

Amit Chigadani