I am fairly new to Python and using Python 3.2. I am trying to write a python script that will pick a file from user machine (such as an image file) and submit it to a server using REST based invocation. The Python script should invoke a REST URL and submit the file when the script is called.
This is similar to multipart POST that is done by browser when uploading a file; but here I want to do it through Python script.
If possible do not want to add any external libraries to Python and would like to keep it fairly simple python script using the core Python install.
Can some one guide me? or share some script example that achieve what I want?
Requests library is what you need. You can install with
pip install requests
.http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
A RESTful way to upload an image would be to use
PUT
request if you know what the image url is:upload_docs.py contains an example how to upload a file as
multipart/form-data
with basic http authentication. It supports both Python 2.x and Python 3.You could use also
requests
to post files as amultipart/form-data
:You can also use unirest . Sample code
You can check out this post http://stackandqueue.com/?p=57 for more details