A
A
Alexander Lebedev2018-01-25 08:26:31
JavaScript
Alexander Lebedev, 2018-01-25 08:26:31

How to correctly convert a request from curl format to Python requests?

Good morning.
I want to make a request to the API, I constantly get an authorization error. What does a curl request look like:

curl \
-H 'Content-Type: application/json' \
-H 'User-Id: %USER_ID%' \
-H 'Authorization: bearer %USER_API_KEY%' \
-d '{"fields":["info","sum","date"],"orders":[{"name":"date","direction":"DESC"}],"limit":"10"}' \
"https://api.topvisor.ru/v2/json/get/bank/history"

What it looks like in HTTP format:
POST /v2/json/get/bank/history HTTP/1.1
Host: api.topvisor.ru
Content-Type: application/json
User-Id: %USER_ID%
Authorization: bearer %USER_API_KEY%

{"fields":["info","sum","date"],"orders":[{"name":"date","direction":"DESC"}],"limit":"10"}

What my requests version looks like:
import requests

url = 'https://api.topvisor.ru/v2/json/get/bank/history'
headers = {
    "Content-type": "application/json",
    "User-Id": "111111",
    "Authorization": "bearer 11111111111111111111"
}

data = {
    "fields": ["info", "sum", "date"],
    "orders": [{
        "name": "date",
        "direction": "DESC"
    }],
    "limit": "10"
}

r1 = requests.get(url, headers=headers, data=data)
r2 = requests.post(url, headers=headers, data=data)

I tried get/post (suddenly I didn't understand the request type), different quotes, changed the file encoding, etc., but I still get:
{"error":true,"message":"not exists POST: \"api_key\""}

That is, not even a request error in the API, but I can’t even get through authorization.
I feel that something is wrong with the syntax or something like that, but I can not understand what exactly. I would be grateful for any advice :)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yana Tomkovich, 2019-11-10
@angela_merkel

If you have gulp 4, then maybe you forgot to use the parallel or series functions

A
Alexander Lebedev, 2018-01-25
@sortarage

There was an internal problem of the service (redirect to the previous version of the API), the code worked, everything is fine :)

N
Nikita, 2018-01-25
@vasilek-nik

There is a good service that allows you to convert a cUrl request into code using the requests module.
Cheers, use:
https://curl.trillworks.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question