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!
Ying xiong
source share