开发者

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: 'sender@email.com', To: 'recipient@email.com', Subject: 'Postmark test', HtmlBody: 'Hello dear Postmark user.'}"


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: '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()

Check out: urllib2 the unwritten manual

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜