I've got a really simple question, but I can't figure it out how to do it. The problem I have is that I want to send the following payload using Python and Requests:
{ 'on': true }
Doing it like this:
payload = { 'on':true } r = requests.put("http://192.168.2.196/api/newdeveloper/lights/1/state", data = payload)
Doesn't work, because I get the following error:
NameError: name 'true' is not defined
Sending the true as 'true' is not accepted by my server, so that's not an option. Anyone a suggestion? Thanks!
Short answer, yes that is the proper way to send the JSON. You should not be placing anything other than a string inside of quotes. As for your bool value, if you want it to convert straight into a bool, than you do not need to include the quotes.
To post a JSON to the server using Python Requests Library, call the requests. post() method and pass the target URL as the first parameter and the JSON data with the json= parameter. The json= parameter takes a dictionary and automatically converts it to a JSON string.
The boolean type matches only two special values: true and false . Note that values that evaluate to true or false , such as 1 and 0, are not accepted by the schema.
You need to json encode it to get it to a string.
import json payload = json.dumps({"on":True})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With