It is possible to return an Observableboolean from the get request

0

I need to check if the api is active. Basically I am trying to check if the request status is equal to 0; if it is, I know that the api is off the air.

But I wanted to put this method in my service class and only return true or false to whoever uses it, without having to call a subscrible and do the checks.

verificarServer(): Observable<boolean> {

    return this.http.get('http://' + this.HOST + ':' + this.PORT + '/' + this.API).subscribe(
      s => {  },
      e => {
        if(e.status == 0){

        }else{

        }
      }
    );

  }

This is a code I'm trying, it would be something more or less that I would need. Something that returns me true or false in an observable.

How could I solve this?

Edit1

The code using the map does not work (example of this response ) I'm using http from HttpClient

 this.http.get('http://' + this.HOST + ':' + this.PORT + '/' + this.API).map((response:Response)=>{
      console.log("imprime alguma coisa aqui Cara#$*&¨#*");
      console.log(response.status);
    });

does not fall within the function map

    
asked by anonymous 12.02.2018 / 21:52

1 answer

0

What's going on here is that you're trying to figure out a "cold Note that this happens when you do not subscribe to an event (in this case, the event that is returned from http.get() .)

This can be solved in three ways:
1) in your component, you do .subscribe() after calling this function
2) in your service, you make a .subscribe () in this function and you send the value by another observable that is made available by the service 3) pass this observable cold onto a Promisse and resolve that promise on the component side

    
10.04.2018 / 17:33