I'm trying to simultaneously send multiple POST requests with JSON payloads and retrieve the JSON results. I've read a few other posts on SO but nothing is doing the trick.
def transcribe(vid_segs):
payloads = []
for vid in vid_segs:
vid = base64.b64encode(open(vid).read())
payload = {
"config": {
"encoding": "LINEAR16",
"sampleRateHertz": 16000,
"languageCode": "en-US",
"speechContexts": {
"phrases:": ["Barack", "Obama", "Barack Obama"]
}
},
"audio": {
"content": vid
}
}
payloads.append(payload)
url = "https://speech.googleapis.com/v1/speech:recognize?key=MYAPIKEY"
unsent_request = []
for payload_single in payloads:
unsent_request.append(grequests.get(url,
params=payload_single))
responses = grequests.map(unsent_request)
for response in responses:
print response.json()
response.close()
If I use response.json(), it returns the error:
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
If I try with response.json, what's returned isn't the actual JSON content, just the object details and the response code:
<bound method Response.json of <Response [404]>>
Any ideas? Thanks in advance!
response.json
is a method, so you need to call it:response.json()
. TheJSONDecodeError
is telling you that the response isn't valid JSON. Try printingresponse.text
(the response as text) orresponse.content
(binary response) to see what the response actually consists of.As @Lohmar ASHAR points out in a comment, you're getting a 404, so it's probably not suprising you're not getting JSON back.
More on responses: http://docs.python-requests.org/en/master/user/quickstart/#response-content