+4 votes
2 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 (25.6k points)
edited ago 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:-

by (6.1k points)
This worked for me. Thank you.
by (36.4k 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 (50.5k points)
Worked for me thanks
+3 votes
by (39.6k 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.


 

by (22k points)
This is the simplest way to use CURL for posting on the server. I would highly recommend this answer.
by (44k points)
Very well explained!
by (32.7k points)
This worked perfectly for what I came looking for.
Thanks!
+4 votes
by (42.2k 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 (26.3k 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 (32.5k 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

by (25.4k 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

 http://onlinedevtools.in/curl
...