0

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!

Steven
  • 824
  • 1
  • 8
  • 23
  • First thing that gets my attention is that 404, maybe the URL is ... wrong, malformed or incomplete? Does sending only 1 payload works ? Seen that you get a `JSONDecodeError` that might be because the response is not JSON (which seems reasonable seen the 404), try debugging/see the `response` object in some other way (print, dir, .. or just use a debugger: pdb or whatever your IDE can offer ) – Lohmar ASHAR May 16 '17 at 20:00

1 Answers1

0

response.json is a method, so you need to call it: response.json(). The JSONDecodeError is telling you that the response isn't valid JSON. Try printing response.text (the response as text) or response.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

Brian from QuantRocket
  • 5,268
  • 1
  • 21
  • 18
  • Thanks Brian and @Lohmar. You're right about the 404 code - it's not actually getting a response from the API. So I guess the question should really be about the way I am going about the request in general. I've verified that everything is good with the URL and the parameters - if I try one at a time, it works fine. – Steven May 16 '17 at 20:16