2017-02-14 36 views
0

我試圖在SpeechMatics API上使用多部分數據提交的請求。使用API​​的多部分/表單數據請求

的API聲明如下在捲曲:

curl -F [email protected]_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

使用請求庫,我的代碼如下,但似乎無法上傳文件:

import Requests 

path = 'https://api.speechmatics.com/v1.0/user/userID/jobs' 
token = {'auth_token':<some token>} 

data_file = open('F:\\user\\Bern\\Data Files\\audio.flac','rb') 
model = 'en-US' 

r = requests.post(path,params=token,files={'data_file':data_file,'model':model}) 

我得到的效應初探200,但文件似乎無法上傳。

+0

嘗試'文件= {「文件」:DATA_FILE,「模式」:模式}' – Juggernaut

+0

嘗試,並沒有工作,捲曲清楚地表明,文件必須與參數「數據文件」被束縛。 – BernardL

回答

0

,我被引導後同時使用的API的數據和文件參數。

下面是我使用的剪切和粘貼代碼。

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 
0

我認爲這是你在找什麼

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) 
相關問題