Intellipaat Back

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

I want to execute a curl command in python.

curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere

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, 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere')
c.setopt(c.WRITEFUNCTION, response.write)
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','Accept-Charset: UTF-8'])
c.setopt(c.POSTFIELDS, '@request.json')
c.perform()
c.close()
print response.getvalue()
response.close()

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('https://github.com/timeline.json')

r.json()

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 = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere'

payload = open("request.json")

headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}

r = requests.post(url, 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
Intellipaat-community
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 - https://curl.trillworks.com/#python.

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!"}' https://google.com/yes

It will convert it to this:

import requests

headers = {

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

}

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

response = requests.post('https://google.com/yes', 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
Intellipaat-community
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.
Thanks!
+4 votes
by (107k 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": "10.0.0.1/32", "nw_dst": "10.0.0.2/32", "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:

    print(x)

f.close()

+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": "10.0.0.1/32", "nw_dst": "10.0.0.2/32", "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: https://docs.python.org/2/library/subprocess.html#popen-constructor

Execute curl command within a Python script
Intellipaat-community
by (29.5k points)
Thanks for the link to the documentation, I resolved a similar issue with it !
by
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

 http://onlinedevtools.in/curl

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...