It is possible to return a Observable<Boolean> from the get request

Asked

Viewed 619 times

2

I need to check if the api is active. Basically I’m trying to check if the request status is 0; if so, I know the api is out of order.

But I wanted to put this method in my service class and only return true or false to whoever is going to use 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{

        }
      }
    );

  }

That’s a code I’m trying to give you, but it would be something else at least as soon as I needed it. Something that returns me true or false in an observable.

How could I fix this ?

Edit1

The code using the map does not work (example of this reply) I’m using the http of 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

  • I think maybe this resolution will help you: https://stackoverflow.com/a/43683143/7857065

  • Thanks, but it didn’t work no, I’m using Httpclient, I think what’s in this answer is Http. Some methods that are in the answer do not even exist for Httpclient. I put the map in the get request to test, apparently I didn’t even fall into the map.

  • You can return using get<boolean>(url)

2 answers

1


After making the request through the method get, use within the pipe one map to check if the status returned is zero by projecting a boolean value. Next use a catchError returning of(false) to handle possible errors. In this format your method will always return a boolean. Use the example below to adapt your service method:

ngOnInit() {
    this.http.get('https://www.mocky.io/v2/5185415ba171ea3a00704eed', { observe: 'response' })
        .pipe(
            map(a => {
                console.log('status', a.status);
                return a.status === 200;
            }),
            catchError(err => {
                console.log('Erro na requisição', err);
                return of(false);
            })
        )
        .subscribe(
            (a: boolean) => console.log('Resultado', a)
        );
}

0

What’s going on here is that you’re trying to take a value from a "Cold Observable" 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 the .subscribe() after calling this function
2) in your service, do a . subscribe() in that function and send the value on the other hand observable that is made available by the service
3) Pass that observable Cold for a Promisse and resolve that promise on the component side

Browser other questions tagged

You are not signed in. Login or sign up in order to post.