Explore Courses Blog Tutorials Interview Questions
+4 votes
in Python by (1.3k points)

I want to execute a curl command in python.

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

There is a request.json file to be sent to get response. I tried to write code, but it does not 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'. Help me fix it.

5 Answers

+4 votes
by (32.3k points)
edited by

Maybe you should consider using the Requests library.

If you simply take an example with JSON response content, it  would be something like:

import requests

r = requests.get('')


For more information just visit the Quickstart section, they will provide you with 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)

You can use the following video tutorials to clear all your doubts:-

If You want to learn python for data science visit this python course by Intellipaat.

Execute curl command within a Python script
by (19.9k points)
This worked for me. Thank you.
by (47.2k points)
It depends on the subprocess, but spawing subprocesses calling commands when the language has core libraries to do so it's not definitely the right way to do it
by (106k points)
Worked for me thanks
+3 votes
by (44.4k points)

This website is suggestable -

This will convert any curl command to Python, Node.js, Rust, JSON, and few more.

If your curl looks something like this:

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

It will convert it to this:

import requests

headers = {

    'Content-type': 'application/json',


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

response ='', headers=headers, data=data)

Also, on the above example, make sure to import json and then use it like this  data=json.dumps(data) for the above case.


Execute curl command within a Python script
by (29.3k points)
This is the simplest way to use CURL for posting on the server. I would highly recommend this answer.
by (62.9k points)
Very well explained!
by (33.1k points)
This worked perfectly for what I came looking for.
+4 votes
by (108k points)

You don't have to use cURL to POST something to a server. requests can do so quite easily (as can urllib, with a bit more effort).

You could use urllib as follows:

import urllib2

data = '{"nw_src": "", "nw_dst": "", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}'

url = 'http://localhost:8080/firewall/rules/0000000000000001'

req = urllib2.Request(url, data, {'Content-Type': 'application/json'})

f = urllib2.urlopen(req)

for x in f:



+2 votes
by (32.1k points)

If you are not adjusting to the curl command properly, calling the curl command directly like so may help you with sorting your query:

import shlex

cmd = '''curl -X POST -d  '{"nw_src": "", "nw_dst": "", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}' http://localhost:8080/firewall/rules/0000000000000001'''

args = shlex.split(cmd)

process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

stdout, stderr = process.communicate()

+1 vote
by (40.7k points)

Try using the code given below instead of using cmd.split() this way:

import shlex

args = shlex.split(cmd)

Then you can feed args to subprocess.Popen.

Check this document for more info:

Execute curl command within a Python script
by (29.5k points)
Thanks for the link to the documentation, I resolved a similar issue with it !
by (100 points)
This site is useful for convert curl command to Http request
1.curl to nodejs
2.curl to php
3.curl to python
4.curl to Rust
5.curl to R

Related questions

0 votes
1 answer
asked Sep 16, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Feb 15, 2021 in SQL by adhiraj (4k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories