More complicated POST requests

Typically, you want to send some form-encoded data — much like an HTML form. To do this, simply pass a dictionary to the data argument. Your dictionary of data will automatically be form-encoded when the request is made:

  1. >>> payload = {'key1': 'value1', 'key2': 'value2'}
  2. >>> r = requests.post("http://httpbin.org/post", data=payload)
  3. >>> print(r.text)
  4. {
  5. ...
  6. "form": {
  7. "key2": "value2",
  8. "key1": "value1"
  9. },
  10. ...
  11. }

There are many times that you want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly.

For example, the GitHub API v3 accepts JSON-Encoded POST/PATCH data:

  1. >>> import json
  2. >>> url = 'https://api.github.com/some/endpoint'
  3. >>> payload = {'some': 'data'}
  4. >>> r = requests.post(url, data=json.dumps(payload))

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:

  1. >>> url = 'https://api.github.com/some/endpoint'
  2. >>> payload = {'some': 'data'}
  3. >>> r = requests.post(url, json=payload)