使用API的多部分/表单数据请求

2024-09-29 23:25:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在SpeechMatics API上使用多部分数据提交请求。在

API在curl中声明如下:

curl -F data_file=@my_audio_file.mp3 -F model=en-US "https://api.speechmatics.com/v1.0/user/17518/jobs/?auth_token=<some token>" # transcription

其中,数据文件应该是本地路径,而模型是此处文档中的语言,https://app.speechmatics.com/api-details#getJobs

使用请求库,我的代码如下,但似乎无法上载文件:

^{pr2}$

我收到回复200,但文件似乎无法上传。在


Tags: 文件数据httpscomtokenapi声明data
2条回答

我在一篇帖子的指导下使用了API的data和files参数。在

下面是我使用的剪切粘贴代码。在

post_api = 'https://api.speechmatics.com/v1.0/user/17518/jobs/?auth_token=<some token>'
path = input()
files = [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.mp3')]

l = []

for file in files:
    with open(file, 'rb') as f: 

        files = {
            'data_file': f 
        }
        data = {'model': 'en-AU'}

        r = requests.post(post_api,data = data,files=files)
        l.append(r)

    f.closed

我想这就是你要找的

import requests

files = {
    'data_file': open('my_audio_file.mp3', 'rb'),
    'model': 'en-US'
}

requests.get('https://api.speechmatics.com/v1.0/user/17518/jobs/?auth_token=<some token>', files=files)

相关问题 更多 >

    热门问题