[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 0However, this doesn't seem to be solving it. Any pointers would be greatly appreciated.
Kind Regards,
Fox -
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 0Solved 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...
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login