The Python Oracle

Using python 'requests' to send JSON boolean

This video explains
Using python 'requests' to send JSON boolean

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Track title: CC O Beethoven - Piano Sonata No 3 in C

--

Chapters
00:00 Question
00:41 Accepted answer (Score 29)
00:55 Answer 2 (Score 5)
01:08 Answer 3 (Score 3)
01:34 Answer 4 (Score 1)
01:50 Thank you

--

Full question
https://stackoverflow.com/questions/1798...

Answer 2 links:
[json]: https://docs.python-requests.org/en/mast...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #rest #pythonrequests

#avk47



ACCEPTED ANSWER

Score 34


You need to json encode it to get it to a string.

import json 
payload = json.dumps({"on":True})



ANSWER 2

Score 7


Starting from requests 2.4.2, instead of passing in the payload with the data parameter, you can use the json parameter like this:

payload = {'on': True}
requests.put(url, json=payload)

And the request will be formatted correctly as a json payload (i.e. {'on': true}).




ANSWER 3

Score 6


should be {'on': True}, capital T




ANSWER 4

Score 1


to make it be lower case like that (if that's what your endpoint requires) do in quotes {'on':'true'}