🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    [Solved]Exceptions & timeouts with GET Requests

    Scheduled Pinned Locked Moved Micropython
    5 Posts 3 Posters 15.5k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F Offline
      Foxhound
      last edited by m5-docs

      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

      m5-docsM 1 Reply Last reply Reply Quote 0
      • m5-docsM Offline
        m5-docs @Foxhound
        last edited by

        @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
        

        M5Stack documentation URL

        https://docs.m5stack.com

        1 Reply Last reply Reply Quote 0
        • F Offline
          Foxhound
          last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • F Offline
            Foxhound
            last edited by

            @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.

            E 1 Reply Last reply Reply Quote 2
            • E Offline
              Efried @Foxhound
              last edited by

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

              1 Reply Last reply Reply Quote 0

              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
              • First post
                Last post