简体   繁体   中英

Azure Cognitive text analytics Tool returns 400 Bad request Python

I am trying to use Azure Text analytics tool to extract topics but getting the 400 Bad request error: My code below:

account_key = '546e6162da424e6f991d03b7f6acxxx'
headers = {
 'Ocp-Apim-Subscription-Key': account_key,
 'Content-Type': 'application/json',
 'Accept': 'application/json'}

import requests

tufani={
     "documents": [
         {
             "language": "en",
             "id": "1",
             "text": "First document"
         },
         {
             "language": "en",
             "id": "100",
             "text": "Final document"
         }
     ]
 }

print('Starting topic detection.')
uri = base_url + 'text/analytics/v2.0/topics'
r=requests.post('https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/topics',data=str(tufani),headers =headers)

print(r.status_code, r.reason)

(400, 'Bad Request')

What am I doing wrong in this?

Thanks!

I tried to reproduce your issue, and I discovered the 400 Bad Request error for your code was caused by as below if using print(r.text) .

u'{"code":"BadRequest","message":"Invalid request","innerError":{"code":"InvalidRequestContent", "message":"Requests to this API should contain at least 100 documents, where each document is not null or empty","minimumNumberOfDocuments":100 }}'

It also show in the offical tutorial Task 3 - Detect topics in a corpus of text , as below.

This API requires a minimum of 100 text records to be submitted, but is designed to detect topics across hundreds to thousands of records. Any non-English records or records with less than 3 words will be discarded and therefore will not be assigned to topics. For topic detection, the maximum size of a single document that can be submitted is 30KB, and the total maximum size of submitted input is 30MB. Topic detection is rate limited to 5 submissions every 5 minutes.

So please add the enough text records for using the API.

You should not use str(tufani) to encode data for POST request, instead requests will automatically encode dict data for you:

r=requests.post('https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/topics',data=tufani,headers =headers)

PS If server accepts JSON-Encoded POST/PATCH data, you could use json.dumps(payload) to do the job.

The solution suggested by Shane and Peter both are the errors I was missing and these suggestion solved the issue.

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM