how to use "azure translate" with curl method

Curl example


curl -X POST "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=ko" -H "Ocp-Apim-Subscription-Key: <secret key>" -H "Ocp-Apim-Subscription-Region: eastus" -H "Content-Type: application/json; charset=UTF-8" -d '[{"Text":"Hello, what is your name?"}]'
 
curl -X POST "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en&to=ko" 
    -H "Ocp-Apim-Subscription-Key: <secret key>" 
    -H "Ocp-Apim-Subscription-Region: eastus" 
    -H "Content-Type: application/json; charset=UTF-8" 
    -d '[{"Text":"Hello, what is your name?"}]'


위에 것으로 실행가능.

아래 MS 공식사이트의 azure ai translate v3.0의 예시가 애매하게 되어 있어서 설명 그대로 설정하면 제대로 curl 명령어로는 동작하지 않는다. 

https://learn.microsoft.com/ko-kr/azure/ai-services/translator/text-translation/reference/v3/translate




Python example

import requests, uuid, json
 
 
# Add your key and endpoint
endpoint = "https://api.cognitive.microsofttranslator.com"
key = "<secret key>"
 
location = "eastus"
# location, also known as region.
# required if you're using a multi-service or regional (not global) resource. It can be found in the Azure portal on the Keys and Endpoint page.
 
path = '/translate'
constructed_url = endpoint + path
 
params = {
    'api-version': '3.0',
    'from': 'en',
    'to': ['fr', 'ko']
}
 
headers = {
    'Ocp-Apim-Subscription-Key': key,
    # location required if you're using a multi-service or regional (not global) resource.
    'Ocp-Apim-Subscription-Region': location,
    'Content-type': 'application/json',
    'X-ClientTraceId': str(uuid.uuid4())
}
 
# You can pass more than one object in body.
body = [{
    'text': 'I would really like to drive your car around the block a few times!'
}]
 
request = requests.post(constructed_url, params=params, headers=headers, json=body)
response = request.json()
 
print(json.dumps(response, sort_keys=True, ensure_ascii=False, indent=4, separators=(',', ': ')))





댓글 쓰기 · 수정

0 댓글