[Solved]Exceptions & timeouts with GET Requests



  • Hi,

    Thanks everyone on this forum for their help. Currently finishing off my project. However, somtimes my GET request fails, and it's causing my device to crash. What's the proper way to set a timeout or catch an error?

    I'm currently trying the following.

    try:
            lResponse = urequests.get( URL , headers = self.fHeaders )
            content = ujson.loads( lResponse.content )
            return abs( content )
        except:
            return 0
    

    However, this doesn't seem to be solving it. Any pointers would be greatly appreciated.

    Kind Regards,
    Fox



  • @foxhound

    try:
            lResponse = urequests.get( URL , headers = self.fHeaders )
            content = ujson.loads( lResponse.content )
            return abs( content )
        except:
            # print("GET failed.") # you need print some information to serial monitor. I just take an example. or write some code for catching timeout error
            pass
    


  • @m5-docs said in Exceptions & timeouts with GET Requests:

    pass

    Thanks for your response, but a little confused with how this differs from mine?

    I'd love to do a timeout, is there a way to write a function that will timeout, similar to eventlib in Python? Or is there a different way I am supposed to be doing a get request that won't block.



  • @m5-docs so it turns out I was forgetting to close the response.

    try:
        lResponse = urequests.get( URL , headers = self.fHeaders )
        content = ujson.loads( lResponse.content )
        lResponse.close()
        return abs( content )
    except:
        return 0
    

    Solved the issue.



  • @foxhound What about adding some hints if it fails return_code for example. I'm asking because the connection seems not to be stable...