1

I am trying to convert a curl request to python request, but i have problem to convert -u .

 curl -X POST \
   -u "apikey:yourKey" \
   --header "Content-Type: audio/wav" \
   --data-binary "@path" \
   "https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize?model=de-DE_BroadbandModel

My solution:

import requests
data = "path"
url = 'https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize?model=de-DE_BroadbandModel'
#payload = open("request.json")
headers = {'content-type': 'audio/wav', 'username': "apikey=yourkey" }
r = requests.post(url, headers=headers, data=data)

Edit:

 import requests
    data = "path"
    url = 'https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize?model=de-DE_BroadbandModel'
    #payload = open("request.json")
    headers = {'Content-Type': 'audio/wav'}
    #r = requests.post(url, headers=headers, data=data)
    print requests.post(url, verify=False, headers=headers, data=data, auth=('apikey', "key"))

now i get

Response [400]

(the curl cmd is working)

Walid Juhu
  • 35
  • 4

1 Answers1

2

Try this

requests.post(url, headers=headers, data=data, auth=(apiKey, yourApiKey))

-u is short for --user which is used for server authentication see here, also look into Basic authentication for requests library.

Edit: You need to read the file (specified in --data-binary "@path") first before passing it in requests.post. I hope this link helps