【问题标题】:how to use curl with -u in python request如何在 python 请求中使用 curl 和 -u
【发布时间】:2019-06-22 14:29:58
【问题描述】:

我正在尝试将 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 正在运行)

【问题讨论】:

    标签: python curl request


    【解决方案1】:

    试试这个

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

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

    编辑:您需要先读取文件(在--data-binary "@path" 中指定),然后再将其传递给requests.post。我希望this链接有帮助

    【讨论】:

      猜你喜欢
      • 2019-07-04
      • 1970-01-01
      • 2021-10-01
      • 2011-09-06
      • 2021-10-26
      • 2016-02-08
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多