Python 3 script to upload a file to a REST URL (multipart request)

33,141

Solution 1

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

>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}
>>> r = requests.post(url, files=files)

Solution 2

A RESTful way to upload an image would be to use PUT request if you know what the image url is:

#!/usr/bin/env python3
import http.client 

h = http.client.HTTPConnection('example.com')
h.request('PUT', '/file/pic.jpg', open('pic.jpg', 'rb'))
print(h.getresponse().read())

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 a multipart/form-data:

#!/usr/bin/env python3
import requests

response = requests.post('http://httpbin.org/post',
                         files={'file': open('filename','rb')})
print(response.content)
Share:
33,141
AniJ
Author by

AniJ

Updated on July 11, 2022

Comments

  • AniJ
    AniJ almost 2 years

    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?

    • AniJ
      AniJ over 12 years
      @J.F.Sebastian : I have already tried link but could not make it work. As I said I am newbie to Python and come from a Java background. I realized that I may not be using the correct libraries required. I will try what you suggested.
    • Kenneth Reitz
      Kenneth Reitz about 12 years
      Requests now supports Python 3.
  • AniJ
    AniJ over 12 years
    No specific reason for 3.2. Being new to Python I decide to get the latest release and did not give much thought the aspects you mentioned. I had seen the MultipartPostHandler before but it did not work because I could not install the .egg file. Is there a different way to install .egg in 3.2? anyway ... let me try with 2.7.
  • Jesvin Jose
    Jesvin Jose over 12 years
    "install .egg in 3.2" you should wait for a port. it depends on urllib2 which (AFAIK, cant substantiate this!) is not forward compatible with Python 3.
  • AniJ
    AniJ over 12 years
    Although both the post were helpful this is more close to my requirements. Thanks for your help.
  • Kenneth Reitz
    Kenneth Reitz about 12 years
    Requests now supports Python 3.
  • Kenneth Reitz
    Kenneth Reitz about 12 years
    Requests now supports Python 3.
  • hlkstuv_23900
    hlkstuv_23900 over 9 years
    Is this solution only for python 3? I am working on python 2.7 and will let you know!
  • jfs
    jfs over 9 years
    @tilaprimera: the question has python-3.x tag therefore the answer is for Python 3 but almost the same code works on Python 2 too (just change the import in the first code example).
  • hlkstuv_23900
    hlkstuv_23900 over 9 years
    Is it done the same way if I am doing request.PUT on an mp3 file?
  • jfs
    jfs over 9 years
    @tilaprimera: yes. You could also use Content-Type if your server supports it (so that you wouldn't put an mp3 file using an url for an image).
  • Arockia
    Arockia about 5 years
    Thanks and It worked for single file. How about multiple files?
  • Jesvin Jose
    Jesvin Jose about 5 years
    @Arockia I would try MultipartEncoder from requests_toolbelt or even uploading in a loop