The Python Oracle

Using python 'requests' to send JSON boolean

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Dreamlands

--

Chapters
00:00 Using Python 'Requests' To Send Json Boolean
00:37 Answer 1 Score 6
00:46 Accepted Answer Score 34
00:57 Answer 3 Score 1
01:08 Answer 4 Score 7
01:27 Thank you

--

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

--

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'}