Skip to content
Advertisement

Convert PHP curl POST request with array of parameters to Python code

I want to convert my php code to python code.

JavaScript

this code successfully fetching data from remote api, but when i try to write it with Python requests – array of parameters is sending with wrong data.

JavaScript

“Parameters”, sended by my Python script is invalid

Advertisement

Answer

PHPs http_build_query and it’s corresponding $_GET and $_POST parsing are completely arbitrary in how they work. Thus you must implement this functionality yourself.

Let’s compare the outputs of PHPs http_build_query to pythons urlencode (which requests uses internally to build the parameters)

PHP

JavaScript

Result:

JavaScript

Python

JavaScript

Result:

JavaScript

This looks quite a bit different but apparently conforms to the standard and thus httpbin interprets this correctly.

To make python behave the same as PHP, I’ve adapted this answer to create the following:

JavaScript

Which outputs

JavaScript

which seems to be what you want.

Now you should be able to pass the result of flatten as data to get the desired behaviour.

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