Make an http POST request to upload a file using Python urllib/urllib2

Ying Xiong picture Ying Xiong · Nov 20, 2014 · Viewed 48.5k times · Source

I would like to make a POST request to upload a file to a web service (and get response) using Python. For example, I can do the following POST request with curl:

curl -F "[email protected]" -F output=json http://jigsaw.w3.org/css-validator/validator

How can I make the same request with python urllib/urllib2? The closest I got so far is the following:

with open("style.css", 'r') as f:
    content = f.read()
post_data = {"file": content, "output": "json"}
request = urllib2.Request("http://jigsaw.w3.org/css-validator/validator", \
                          data=urllib.urlencode(post_data))
response = urllib2.urlopen(request)

I got a HTTP Error 500 from the code above. But since my curl command succeeds, it must be something wrong with my python request?

I am quite new to this topic and my question may have very simple answers or mistakes.

Answer

Wolph picture Wolph · Nov 20, 2014

Personally I think you should consider the requests library to post files.

url = 'http://jigsaw.w3.org/css-validator/validator'
files = {'file': open('style.css')}
response = requests.post(url, files=files)

Uploading files using urllib2 is not impossible but quite a complicated task: http://pymotw.com/2/urllib2/#uploading-files