如何在python请求中与-u一起使用curl

时间:2019-01-29 12:46:51

标签: python curl request

我正在尝试将curl请求转换为python请求,但是在转换-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

我的解决方案:

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)

编辑:

 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"))

现在我得到

  

响应[400]

(curl cmd正在工作)

1 个答案:

答案 0 :(得分:1)

尝试

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

-u是--user的缩写,用于服务器身份验证see here,也可以查看Basic authentication中的请求库。

编辑:在将文件传递到--data-binary "@path"之前,您需要先阅读文件(在requests.post中指定)。希望this链接有帮助