Home > Mobile >  Convert request from curl to python
Convert request from curl to python

Time:06-02

I have this request using a curl command and I want to translate it to python using the requests library

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"}'

CodePudding user response:

You can use requests library.

The following curl:

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"
                }'

I wrote in python the following way:

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']

If this answer was helpful, please upvote and accept it.

Good luck :)

CodePudding user response:

You can try this one.

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)
  • Related