我必须在python中发送post请求以获取以下数据
curl -X POST -u "{username}":"{password}"
--header "Content-Type: audio/flac"
--data-binary "@audio-file1.flac"
"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&word_alternatives_threshold=0.9&keywords=%22colorado%22%2C%22tornado%22%2C%22tornadoes%22&keywords_threshold=0.5"
答案 0 :(得分:1)
您可以先阅读post requests
的文档。
这将向您的curl
命令发送类似的请求:
import requests
url = 'https://stream.watsonplatform.net/.......'
auth = ('{username}', '{password}')
headers = {'Content-type': 'audio/flac'}
with open('audio-file1.flac', 'rb') as f:
r = requests.post(url, auth=auth, headers=headers, data=f)
部分标题会有所不同,例如User-Agent
,但请求是相同的。
您可以改为使用字典并将其传递给requests.post()
,而不是在查询字符串中手动放置和转义查询参数:
url = 'https://stream.watsonplatform.net/speech-to-text/api/v1/recognize'
params = {
'keywords': '"colorado","tornado","tornadoes"',
'keywords_threshold': '0.5',
'timestamps': 'true',
'word_alternatives_threshold': '0.9'
}
with open('audio-file1.flac', 'rb') as f:
r = request.post(url, params=params, auth=auth, headers=headers, data=f)