Solved: How to POST JSON data with Python Requests?


I need to POST a JSON from a client to a server. I’m using Python 2.7.1 and simplejson. The client is using Requests. The server is CherryPy. I can GET a hard-coded JSON from the server (code not shown), but when I try to POST a JSON to the server, I get “400 Bad Request”.
Here is my client code:
Here is the server code.
Any ideas?

Best Answer:

Starting with Requests version 2.4.2, you can use the json= parameter (which takes a dictionary) instead of data= (which takes a string) in the call:

If you have better answer, please add a comment about this, thank you!