Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert curl example to pycurl

Could someone convert the following PostMark curl example to pycurl?

curl -X POST "http://api.postmarkapp.com/email" \

-H "Accept: application/json" \

-H "Content-Type: application/json" \

-H "X-Postmark-Server-Token: ed742D75-5a45-49b6-a0a1-5b9ec3dc9e5d" \

-v \

-d "{From: '[email protected]', To: '[email protected]', Subject: 'Postmark test', HtmlBody: '<html><body><strong>Hello</strong> dear Postmark user.</body></html>'}"
like image 978
Darth Avatar asked Apr 02 '14 00:04

Darth


People also ask

How do you convert curl to code in Python?

Curl Converter automatically generates valid Python code using the Python request library for all provided Curl HTTP headers and Curl data. Enter the Curl command, click Run to execute the command online and check the results. Click Generate Code and select Python to convert the Curl command to Python code.

Does Python request use curl?

In Python, cURL transfers requests and data to and from servers using PycURL. PycURL functions as an interface for the libcURL library within Python. Almost every programming language can use REST APIs to access an endpoint hosted on a web server.


1 Answers

You can use something like this. It's a basic implementation but it should work.

import pycurl, json

postmark_url = 'https://api.postmarkapp.com/email'

data = json.dumps({"From": "[email protected]", "To": "[email protected]", "Subject": "Pycurl", "TextBody": "Some text"})

c = pycurl.Curl()
c.setopt(pycurl.URL, github_url)
c.setopt(pycurl.HTTPHEADER, ['X-Postmark-Server-Token: API_TOKEN_HERE','Accept: application/json'])
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)
c.perform()
like image 118
JP Toto Avatar answered Sep 28 '22 08:09

JP Toto