text: Why does HTTP POST request body need to be JSON enconded in Python? - Stack Overflow
  1 I ran into this issue when playing around with an external API. I was sending my body data as a dictionary straight into the request and was getting 400 errors:
  When I added a json.dumps wrap, it works:
  I don't entirely understand why this is necessary, as dictionaries and JSON objects are syntactically identical. Can someone help me understand what is going on behind the scenes here?
  For completeness, here are my headers:
  I didn't mention this earlier but now I feel that it may be relevant. I am using the Python Requests library, and another post seems to suggest that you should never have to encode parameters to a request object: http://stackoverflow.com/a/14804320/1012040
  "Regardless of whether GET/POST you never have to encode parameters again, it simply takes a dictionary as an argument and is good to go."
  Seems like serialization shouldn't be necessary?
  python http python-requests share|improve this question edited Mar 29 '13 at 0:07
  it's not a public API, o/w I would :( Are you saying that this could just be a function of the specific API as opposed to something general to HTTP requests?
  I suggest reading up on object serialization. json.dump serializes the object so that it can be sent as a data stream, in essence. Inserting the object directly will send across a binary stream of data that the server won't know how to decode.
  repr(dict) is not identical to json because the former uses single quotes.
  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.
  >>> 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
  Although they seem syntatically itentical there is a difference: JSON is a string representation of serialized object; in this case Python dict. In this example you need to send serialized data in a form of string and thus json.dumps is necessary to perform the serialization.
  As suggested in comments to the question it is relative to used API, but nevertheless the serialization must be done somewhere along the way to send an object over the wire.
  Not the answer you're looking for?	Browse other questions tagged python http python-requests or ask your own question.	asked