Search code examples
pythonazurehttpazure-functionsspeech-to-text

Error while making azure speech to text api call via python


I am trying to use Azure speech to text api, through python. Below is the code.

import http.client, urllib.request, urllib.parse, urllib.error, base64

headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': '<my_subscription_key>',
}

params = urllib.parse.urlencode({
  "contentUrls": [
      "<URI_TO_MY_AUDIO_FILE>",
  ],
  "properties": {
  },
  "locale": "en-US",
  "displayName": "Transcription"
})

try:
    conn = http.client.HTTPSConnection('eastus.api.cognitive.microsoft.com')
    conn.request("POST", "/speechtotext/v3.0/transcriptions?%s" % params, "{body}", headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

The error I am getting is:

b'{\r\n  "code": "InvalidPayload",\r\n  "message": "Invalid JavaScript property identifier character: }. Path \'\', line 1, position 5."\r\n}'

I believe I am setting my parameters wrong. Unable to find what's wrong.

Python version 3.7

Strangely the same HTTP request is successful when done via https://eastus.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/operations/CreateTranscription


Solution

  • Your POST request body is not supplied in the right format.
    Please see the corrected code below:

    import http.client
    import json
    
    headers = {
        # Request headers
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': '<my_subscription_key>',
    }
    
    body = {
      "contentUrls": [
          "<URI_TO_MY_AUDIO_FILE>",
      ],
      "properties": {
      },
      "locale": "en-US",
      "displayName": "Transcription"
    }
    
    try:
        conn = http.client.HTTPSConnection('eastus.api.cognitive.microsoft.com')
        conn.request("POST", "/speechtotext/v3.0/transcriptions", body=json.dumps(body), headers=headers)
        response = conn.getresponse()
        data = response.read()
        print(data)
        conn.close()
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))
    

    Thanks,
    Sen