python-requests Sending and receiving JSON POSTing JSON


Example

To POST a JSON body, pass in a Python data structure to the json argument; here a dictionary is posted but anything that can be encoded to JSON will do:

import requests

# Create a dictionary to be sent.
json_data = {'foo': ['bar', 'baz'], 'spam': True, 'eggs': 5.5}

# Send the data.
response = requests.post(url='http://example.com/api/foobar', json=json_data)
print("Server responded with %s" % response.status_code)

requests takes care of encoding to JSON for you, and sets the Content-Type to application/json.