How to use Pthon to execute a cURL command?

Posted on

Question :

How to use Pthon to execute a cURL command?

I want to execute a curl command in Python.

Usually, I just need to enter the command in the terminal and press the return key. However, I don’t know how it works in Python.

The command shows below:

curl -d @request.json --header "Content-Type: application/json"

There is a request.json file to be sent to get a response.

I searched a lot and got confused. I tried to write a piece of code, although I could not fully understand it and it didn’t work.

import pycurl
import StringIO

response = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, '')
c.setopt(c.WRITEFUNCTION, response.write)
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','Accept-Charset: UTF-8'])
c.setopt(c.POSTFIELDS, '@request.json')
print response.getvalue()

The error message is Parse Error. How to get a response from the server correctly?

Asked By: Qiang Fu


Answer #1:

For sake of simplicity, maybe you should consider using the Requests library.

An example with json response content would be something like:

import requests
r = requests.get('')

If you look for further information, in the Quickstart section, they have lots of working examples.


For your specific curl translation:

import requests
url = ''
payload = open("request.json")
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
r =, data=payload, headers=headers)
Answered By: otorrillas

Answer #2:

Just use this website. It’ll convert any curl command into Python, Node.js, PHP, R, or Go.


curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}'

Becomes this in Python,

import requests

headers = {
    'Content-type': 'application/json',

data = '{"text":"Hello, World!"}'

response ='', headers=headers, data=data)
Answered By: Kyle Bridenstine

Answer #3:

import requests
url = ""
data = requests.get(url).json


if you are trying to send a file

files = {'request_file': open('request.json', 'rb')}
r =, files=files)
print r.text, print r.json

ahh thanks @LukasGraf now i better understand what his original code is doing

import requests,json
url = ""
my_json_data = json.load(open("request.json"))
req =,data=my_json_data)
print req.text
print req.json # maybe? 
Answered By: Joran Beasley

Answer #4:

curl -d @request.json --header "Content-Type: application/json"

its python implementation be like

import requests

headers = {
    'Content-Type': 'application/json',

params = (
    ('key', 'mykeyhere'),

data = open('request.json')
response ='', headers=headers, params=params, data=data)

#NB. Original query string below. It seems impossible to parse and
#reproduce query strings 100% accurately so the one below is given
#in case the reproduced version is not "correct".
# response ='', headers=headers, data=data)

check this link, it will help convert cURl command to python,php and nodejs

Answered By: cryptoKTM

Answer #5:

My answer is WRT python 2.6.2.

import commands

status, output = commands.getstatusoutput("curl -H "Content-Type:application/json"" -k -u (few other parameters required) -X GET -s"")

Leave a Reply

Your email address will not be published.