Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Post in python. What I did wrong? :(

I have wrote this code for raspberry pi pico W
using those libraries:

import rp2
import network
import ubinascii
import machine
import urequests as requests
import time
import socket

The code :

headers = {"content-type: application/json"}
payload = {"DeviceID": 1,
           "temp":1,
           "humidity":0,
           "data1":0}
print(payload)
print("Executing POST")
response = requests.post(url2, headers=headers, data=payload)
print("sent (" + str(response.status_code) + "), status = " + str(wlan.status()) )
print("POST execution succes")
response.close()

I successfully made a get request.
And I got lots of errors trying POST in Thonny compiler. I have tried to change a lot of things and this is the final form that doesn’t work :).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Okay so you pointed out the error was:

TypeError: 'set' object isn't subscriptable

When looking to see where you defined a set, I saw this set definition:

headers = {"content-type: application/json"}

You surely meant to define it as a dictionary – you’re missing a couple of quotes:

headers = {"content-type": "application/json"}

Both sets and dicts are defined via curly braces; you were defining a set because you weren’t using dict notation, which requires the : to not be contained within a string, like you had.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading