Why does HTTP POST request body need to be JSON enconded in Python?

Apparently your API requires JSON-encoded and not form-encoded data. When you pass a dict in as the data parameter, the data is form-encoded. When you pass a string (like the result of json.dumps), the data is not form-encoded.

Consider this quote from the requests documentation:

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.

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:

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

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

Refs:

  • http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.4
  • http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)