🤖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

    Micropython
    3
    5
    12.3k
    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
      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
        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
          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
            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
              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
              • First post
                Last post