Make a http post request to download the file using python urllib / urllib2

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

 curl -F "file=@style.css" -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 500 HTTP error from the above code. But since my curl command succeeds, should there be something wrong with my python request?

I am new to this thread and please forgive me if the rookie question has very simple answers or errors. Thank you in advance for your help!

+8
python post urllib2 urllib
source share
2 answers

After some digging, this post seems to have solved my problem. It turns out that I need to correctly configure the multi-channel encoder.

 from poster.encode import multipart_encode from poster.streaminghttp import register_openers import urllib2 register_openers() with open("style.css", 'r') as f: datagen, headers = multipart_encode({"file": f}) request = urllib2.Request("http://jigsaw.w3.org/css-validator/validator", \ datagen, headers) response = urllib2.urlopen(request) 
+8
source

Personally, I think you should consider the requests library for publishing 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 rather challenging: http://pymotw.com/2/urllib2/#uploading-files

+12
source

All Articles