Bonsoir,

Je suis débutant dans le langage de Python et j'obtiens cette erreur quand j'exécute mon fichier Translate.py :

{

"error": {

"code": 400074,

"message": "The body of the request is not valid JSON."

}

}

Voici mon fichier Translate.py :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os, requests, uuid, json
 
key_var_name = 'TRANSLATOR_TEXT_SUBSCRIPTION_KEY'
if not key_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(key_var_name))
subscription_key = os.environ[key_var_name]
 
endpoint_var_name = 'TRANSLATOR_TEXT_ENDPOINT'
if not endpoint_var_name in os.environ:
    raise Exception('Please set/export the environment variable: {}'.format(endpoint_var_name))
endpoint = os.environ[endpoint_var_name]
 
path = '/translate?api-version=3.0'
params = '&to=de&to=it'
constructed_url = endpoint + path + params
 
headers = {
    'Ocp-Apim-Subscription-Key': subscription_key,
    'Content-type': 'application/json',
    'X-ClientTraceId': str(uuid.uuid4())
}
 
with open('data.json') as json_data:
    data_dict = json.load(json_data)
    body = json.dumps(data_dict)
 
request = requests.post(constructed_url, headers=headers, json=body)
response = request.json()
 
print(json.dumps(response, sort_keys=True, indent=4, separators=(',', ': ')))
Et le fichier data.json :

Code json : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
[
 
  {
 
    "text": "The diary of Thomas Pereira"
 
  }
 
]

Pourriez-vous me dire ce qu'il bloque dans le code d'après-vous ?

Merci pour votre aide.