Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 571
Bailey Smith Avatar asked Oct 28 '25 01:10

Bailey Smith


1 Answers

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>"
  },
  ...
}
like image 127
Martijn Pieters Avatar answered Oct 30 '25 21:10

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!