Search code examples
pythonapiutf-8python-requestsdeepl

How manage a API (DeepL) which send data in UTF-8 format?


I use the DeepL API : https://www.deepl.com/docs-api/translating-text/

I interact with it by using python and request library.

import requests
import ast
r =  requests.post(url='https://api.deepl.com/v2/translate',
                   data = {
                        'target_lang' : 'FR',  
                        'auth_key' : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                        'text': ''Honey Was ist denn los hier?''})
print(ast.literal_eval(r.text)['translations'][0]['text'])

Output :

'Chérie, que se passe-t-il ici ?'

As you see i get 'Chérie' instead of 'Chérie'

And it's normal because the DeepL API : "Only UTF8-encoded plain text is supported."

But i have no idea to get a correct text (here 'Chérie'), i try some tools like '.encode('utf-8')' in the input but it does'nt work.

Have you an idea ? Thank you in advance :)


Solution

  • I used :

    import requests 
    import ast 
    r = requests.post(url='api.deepl.com/v2/translate', 
          data = { 'target_lang' : 'FR', 
                   'auth_key' : 'xxxxxxxxxxxxxxxxxxxxxxxxxxx', 
                   'text': 'Honey Was it denn los hier?'}) 
    
    print(r.json()['translations'][0]['text'])
    

    And i get :

    'Chérie, que se passe-t-il ici ?

    Thanks lenz ;)