Skip to content
Advertisement

Python requests module sends JSON string instead of x-www-form-urlencoded param string

I was under the impression that POSTSs using x-www-form-urlencoded specifications should send a URL encoded param string in the body of the post. However, when I do this

JavaScript

The body of the request on the receiving end looks like this:

JavaScript

But I was expecting to get this

JavaScript

How I can get Requests to send the data in the second form?

Advertisement

Answer

The reason you’re getting JSON is because you’re explicitly calling json.dumps to generate a JSON string. Just don’t do that, and you won’t get a JSON string. In other words, change your first line to this:

JavaScript

As the docs explain, if you pass a dict as the data value, it will be form-encoded, while if you pass a string, it will be sent as-is.


For example, in one terminal window:

JavaScript

In another:

JavaScript

In the first terminal, you’ll see that the first request body is this (and Content-Type application/x-www-form-urlencoded):

JavaScript

… while the second is this (and has no Content-Type):

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement