Using requests for multipart/form data for API

时间:2017-02-14 09:09:52

标签: python python-requests

I am trying to use requests for a multipart data submission on SpeechMatics API.

The API is declared like this in 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

Where data file is supposed to be the local path and model is the language as per the documentation here, 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})

我收到了Reponse 200,但文件似乎无法上传。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

我受到帖子的指导,同时使用API​​的数据和文件参数。

以下是我使用的剪切和粘贴代码。

class Parent extends PureComponent {
  constructor(props) {
     super(props);
     this.state = {
        content: <div></div>
     }
  }
  componentWillReceiveProps(nextProps) {
     this.setState({
        content: <div>Hello World</div>
     })
  }
  render() {
    return (
      <Child
        content={this.state.content}
      />
    );
  }
}

class Child extends PureCompoenent {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div
        contentEditable="true"
      >
         {this.props.content}
      </div>
    )
  }
}