Menu Close

How to send file using POST from a Python script?

Sometimes, we want to send file using POST from a Python script.

In this article, we’ll look at how to send file using POST from a Python script.

How to send file using POST from a Python script?

To send file using POST from a Python script, we call request.post with the files argument.

For instance, we write

with open('report.xls', 'rb') as f:
    r = requests.post('http://httpbin.org/post', files={'report.xls': f})

to open the report.xls file with open.

Then we call requests.post with the URL we want to send the file to via a POST request.

And we set the files argument to a dict with the key for the file and the file f itself.

Conclusion

To send file using POST from a Python script, we call request.post with the files argument.

Posted in Python, Python Answers