M
M
Mikhail Smykov2017-01-27 14:18:23
Python
Mikhail Smykov, 2017-01-27 14:18:23

Requests: HTTP for humans can't send POST?

Hello. There was a need through Python to send a request to the API server with the current XOR encryption technology. The original function is in PHP, and it looks like this:

$myCurl = curl_init();
curl_setopt_array($myCurl, array(
    CURLOPT_URL => 'http://example.ru/method/Test',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $hashed,
));
$response = curl_exec($myCurl);
curl_close($myCurl);

$hashed - encrypted JSON message for server API. It is passed in the form of a string (not a dictionary or an array, a string)
I'm trying to do the same in Python: (requests library)
request = requests.post("http://example.ru/method/Test", data=hashed)
print(request.text)

The result is an empty utf-8 string. If POST data is not sent, it returns JSON with an error description that we are not sending the right one.
Can you please tell me if I am passing the string correctly? I have no idea how to pass this parameter.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom Innokentiev, 2017-01-27
@artinnok

hashed must be a pythonic dictionary - see documentation

R
Roman Mindlin, 2017-01-27
@kgbplus

request = requests.post("http://example.ru/method/Test", data=dict(s.split("=") for s in hashed.split("&")))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question