簡體   English   中英

將請求從 curl 轉換為 python

[英]Convert request from curl to python

我使用 curl 命令有這個請求,我想使用 requests 庫將它轉換為 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM