将请求从 curl 转换为 python

Convert request from curl to python

我使用 curl 命令收到此请求,我想使用请求库

将其转换为 python
curl -X POST https://endpoint/prod/api/Translations/start \
    -H 'Authorization: Bearer <accessToken>' \
    -H 'Content-Type: application/json' \
    -d '{ "text": ["first segment to translate.", "second segment to translate."], "sourceLanguageCode": "en", "targetLanguageCode": "de", "model": "general", "useCase": "testing"}'

您可以使用 requests 库。

以下卷曲:

curl -X POST "https://www.magical-website.com/api/v2/token/refresh/" \
                -H 'accept: application/json' \
                -H 'Content-Type: application/json' \
                -d '{
                    "refresh": "$REFRESH_TOKEN"
                }'

我在python中是这样写的:

import requests

def get_new_token():
    url = 'https://www.magical-website.com/api/v2/token/refresh/'
    token = constants.THE_TOKEN
    payload = f'{{ "refresh": "{token}" }}'
    headers = {"accept": "application/json", "Content-Type": "application/json"}
    print("Token handling ....")
    r = requests.post(url, data=payload, headers=headers)
    print(f"Token status: {r.status_code}")
    return r.json()['access']

如果这个回答有帮助,请点赞采纳

祝你好运:)

你可以试试这个。

import requests

url = "https://endpoint/prod/api/Translations/start"
payload = {
    "text": ...,
    "sourceLanguageCode": ...,
    ...   
}
headers = { "Authorization": "Bearer ...", "Content-Type": "application/json" }

res = requests.post(url, data = payload, headers = headers)

print(res.status_code)
print(res.text)