Categories
Python Answers

How to send file using POST from a Python script?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *