Question

I am a complete Python noob and am trying to run cURL equivalents using urllib2. What I want is a Python script that, when run, will do the exact same thing as the following cURL command in Terminal:

curl -k -F docfile=@myLocalFile.csv http://myWebsite.com/extension/extension/extension

I found the following template on a tutorial page:

import urllib
import urllib2


url = "https://uploadWebsiteHere.com"
data = "{From: 'sender@email.com', To: 'recipient@email.com', Subject:   'Postmark test', HtmlBody: 'Hello dear Postmark user.'}"
headers = { "Accept" : "application/json",
        "Conthent-Type": "application/json",
        "X-Postmark-Server-Token": "abcdef-1234-46cc-b2ab-38e3a208ab2b"}
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

but I am completely lost on the 'data' and 'headers' vars. The urllib2 documentation (https://docs.python.org/2/library/urllib2.html) defines the 'data' input as "a string specifying additional data to send to the server" and the 'headers' input as "a dictionary". I am totally out of my depth in trying to follow this documentation and do not see why a dictionary is necessary when I could accomplish this same task in terminal by only specifying the file and URL. Thoughts, please?

Was it helpful?

Solution

The data you are posting doesn't appear to be valid JSON. Assuming the server is expecting valid JSON, you should change that.

Your curl invocation does not pass any optional headers, so you shouldn't need to provide much in the request. If you want to verify the exact headers you could add -vi to the curl invocation and directly match them in the Python code. Alternatively, this works for me:

import urllib2

url = "http://localhost:8888/"
data = '{"From": "sender@email.com", "To": "recipient@email.com", "Subject": "Postmark test", "HtmlBody": "Hello dear Postmark user."}'
headers = {
    "Content-Type": "application/json"
}
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

It probably is in your best interest to switch over to using requests, but for something this simple the standard library urllib2 can be made to work.

OTHER TIPS

What I want is a Python script that, when run, will do the exact same thing as the following cURL command in Terminal:

$ curl -k -F docfile=@myLocalFile.csv https://myWebsite.com/extension...

curl -F sends the file using multipart/form-data content type. You could reproduce it easily using requests library:

import requests # $ pip install requests

with open('myLocalFile.csv','rb') as input_file:
    r = requests.post('https://myWebsite.com/extension/...', 
                      files={'docfile': input_file}, verify=False)

verify=False is to emulate curl -k.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top