blackstar012
blackstar012

Reputation: 37

ionic 3 HttpClient get status + send headers

i'm using the HttpClientModule on ionic 3, and i want to do a get on my api

let email = "[email protected]";
let password = "password";
    let headers = new HttpHeaders();
    this.http.get('http://127.0.0.1:8000/api/login',{
      headers: {'email':email,'password':password}
   });

i don't want to get the json but only the status of the request, for doing something like :

if(status == 200) { ... }
esle { ... } 

can you guys please help me ?

Thank you

Upvotes: 1

Views: 6586

Answers (2)

Rohit Kavthekar
Rohit Kavthekar

Reputation: 339

Add observe : 'response' in header options of http.get method and subscribe to get() method to get response status whatever you want..

this.http.get('http://127.0.0.1:8000/api/login',{
      headers: {'email':email,'password':password},observe: 'response'
   }).subscribe(
    res => { console.log(res) ;

             if(res.status==201)
             {
                 //do action
             }else
             {
             }
          },

Upvotes: 2

Pardeep Jain
Pardeep Jain

Reputation: 86750

You have to use observe: 'response' as you second parameter in the Get request of httpClientModule like this -

this.http.get('http://127.0.0.1:8000/api/login',{
      headers: {'email':email,'password':password},
      observe: 'response'
   }

observe : 'response' for complete response.

observe : 'body' for response with body.

observe : 'events' for response with events. For more details on this refer here

Upvotes: 0

Related Questions