0

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

3 Answers 3

2

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']
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

You can use curlify2 to convert request for curl command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.