Scott
Scott

Reputation: 469

Convert CURL command line to Python script

Having way too much trouble making this CMD line curl statement work in python script...help! Attempting to use Urllib.

curl -X POST "http://api.postmarkapp.com/email" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Postmark-Server-Token: abcdef-1234-46cc-b2ab-38e3a208ab2b" \
-v \
-d "{From: '[email protected]', To: '[email protected]', Subject: 'Postmark test', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'}"

Upvotes: 3

Views: 6633

Answers (1)

Joshkunz
Joshkunz

Reputation: 6055

Ok so you should probably user urllib2 to submit the actual request but here is the code:

import urllib
import urllib2

url = "http://api.postmarkapp.com/email"
data = "{From: '[email protected]', To: '[email protected]', 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()

Check out: urllib2 the unwritten manual

I get a 401 unauthorized response so I guess it works :)

Upvotes: 6

Related Questions