Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    JSON posts

    Scheduled Pinned Locked Moved Support & Troubleshooting
    requestsurequestsmicropython
    4 Posts 2 Posters 898 Views 2 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.
    • K Offline
      kjm
      last edited by

      I use this code in python on a rpi to do JSON posts

      import requests
      payload = '{"msg_type":"observation_payload","msg":{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload test","t":"1543540780"}}}}'
      r = requests.post('https://my.website/api/observations', data = {'post_wrapper':payload})
      print r.text
      

      & it prints

      {"msg": {"remote_pi": "FZtank", "stored_ids": ["8"]}, "msg_type": "receipt"}
      

      I figured it wouldn't be too hard to do the same thing in micropython on pycom device

      import urequests as requests
      payload = '{"msg_type":"observation_payload","msg":{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload test","t":"1543540780"}}}}'
      r = requests.post('https://my.website/api/observations', data = {"post_wrapper":payload})
      print(r.text)
      r.close()
      

      but it gives

      Traceback (most recent call last):
        File "<stdin>", line 12, in <module>
        File "/flash/lib/urequests.py", line 115, in post
        File "/flash/lib/urequests.py", line 100, in request
        File "/flash/lib/urequests.py", line 79, in request
      TypeError: object with buffer protocol required
      

      The TypeError msg means it wants a string instead of a dictionary for the data so I tried

      import urequests as requests
      payload = '{"post_wrapper":{"msg_type":"observation_payload","msg":{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload test","t":"1543540780"}}}}}'
      r = requests.post('https://my.website/api/observations', data =payload)
      print(r.text)
      r.close()
      

      but that gives

      {"msg": {"errorText": "Malformed POST - unable to decode JSON"}, "msg_type": "error"}
      

      So now I'm confused. Is the problem that the website expects a dictionary & micropython can only send a string?

      E 1 Reply Last reply Reply Quote 0
      • E Offline
        Eric73 @kjm
        last edited by

        @kjm Have you try something like this ?

        import urequests as requests
        import ujson
        payload = '{"msg_type":"observation_payload","msg":{"serial":"FZtank","observations":{"8":{"d":0,"m":"JSON upload test","t":"1543540780"}}}}'
        r = requests.post('https://my.website/api/observations', data = ujson.dumps({"post_wrapper":payload}))
        print(r.text)
        r.close()
        

        Micro python is a small python sub part, so it have smaller library than python but ujson must do the job you request. (where urequests come from it's not a pycom library isn't it ?)
        If it come from https://github.com/micropython/micropython-lib/blob/master/urequests/urequests.py
        then you can just do
        r = requests.post('https://my.website/api/observations', json = {"post_wrapper":payload})

        1 Reply Last reply Reply Quote 0
        • K Offline
          kjm
          last edited by kjm

          It doesn't like ujson.dumps either

          Traceback (most recent call last):
            File "<stdin>", line 11, in <module>
            File "/flash/lib/urequests.py", line 115, in post
            File "/flash/lib/urequests.py", line 52, in request
          OSError: 128
          

          int(port) in the library seems harmless enough?

          49   if ":" in host:
          50       host, port = host.split(":", 1)
          51       port = int(port)
          52    ai = usocket.getaddrinfo(host, port)
          53    #ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
          

          I'm using the library you linked to (albeit with line 53 changed to stop it crashing) but it won't do json = {"post_wrapper":payload}) either, says TypeError: 'module' object is not callable

          E 1 Reply Last reply Reply Quote 0
          • E Offline
            Eric73 @kjm
            last edited by

            @kjm Line 52 doesn't seem to be related to datapayload, so i suppose that data = ujson.dumps({"post_wrapper":payload})) do the work.

            I don't know what OSError 128 mean but the line seem to be related to networking. Does your software have do correctly the connect with your wifi hotspot before trying to send your request ?

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

            Pycom on Twitter

            Pycom