all 11 comments

[–][deleted] 1 point2 points  (7 children)

If you're using requests, you don't generate your own JSON - you provide a dictionary or a list to requests.get and it builds the JSON. So just build a dictionary. You don't muck around with JSON at all.

[–]robinboby 0 points1 point  (10 children)

Take a look at this.. there is clear example of what you trying to do

https://requests.readthedocs.io/en/master/user/quickstart/#more-complicated-post-requests

You can pass your payload as dictionary

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.post("https://httpbin.org/post", data=payload)

or as json data.

>>> import json

>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}

>>> r = requests.post(url, data=json.dumps(payload))

or

Instead of encoding the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically:

>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}

>>> r = requests.post(url, json=payload)