Bailey Smith
Bailey Smith

Reputation: 2933

Is it possible to post audio files with the python requests library

I'm considering using the Python Requests library to post mp3s to an api, but all the examples of posting files in the documentation are for text files. Is it possible to use this library for audio?

Upvotes: 4

Views: 9289

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1125368

Yes, it is possible to send any sequence of bytes with the library:

with open(audiofile, 'rb') as fobj:
    requests.post(url, files={'fieldname': fobj})

In fact, the first multipart-encoded file example in the requests documentation posts a binary file:

>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}

>>> r = requests.post(url, files=files)
>>> r.text
{
  ...
  "files": {
    "file": "<censored...binary...data>"
  },
  ...
}

Upvotes: 6

Related Questions