Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I want to execute a curl command in python.

Usually, I just need to enter the command in terminal and press 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 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'.Can anyone tell me how to fix it? or how to get a response from the server correctly?

1 Answer

0 votes
by (106k points)

You can use the following code to execute a curl command:-

import requests 

r = requests.get('') 


Related questions

+4 votes
5 answers
asked May 24, 2019 in Python by Aditya98 (1.3k points)
0 votes
1 answer
0 votes
1 answer
asked Dec 29, 2020 in Python by ashely (50.2k points)
0 votes
1 answer

Browse Categories