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 can use the requests
module.
For instance, we write:
import requests
with open('test1.png', 'rb') as f:
r = requests.post('http://httpbin.org/post', files={'test1.jpg': f})
We open the file with open
.
And we open the file with read permission with 'rb'
.
Then we call requests.post
to make a POST request.
And we set the files
parameter to a dictionary containing the file to send the file as the request payload.
Conclusion
To send file using POST from a Python script, we can use the requests
module.