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')
c.perform()
c.close()
print response.getvalue()
response.close()The error message is Parse Error. How to get a response from the server correctly?
11 Answers
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(')
r.json()If you look for further information, in the Quickstart section, they have lots of working examples.
EDIT:
For your specific curl translation:
import requests
url = '
payload = open("request.json")
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
r = requests.post(url, data=payload, headers=headers) 8 Just use this website. It'll convert any curl command into Python, Node.js, PHP, R, or Go.
Example:
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 = requests.post(' headers=headers, data=data) 1 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 = requests.post(' 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 = requests.post(' headers=headers, data=data)check this link, it will help convert cURl command to python,php and nodejs
import requests
url = ""
data = requests.get(url).jsonmaybe?
if you are trying to send a file
files = {'request_file': open('request.json', 'rb')}
r = requests.post(url, files=files)
print r.text, print r.jsonahh 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 = requests.post(url,data=my_json_data)
print req.text
print
print req.json # maybe? 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")
print outputI apologize for not providing the required parameters 'coz it's confidential.
2Some background: I went looking for exactly this question because I had to do something to retrieve content, but all I had available was an old version of python with inadequate SSL support. If you're on an older MacBook, you know what I'm talking about. In any case, curl runs fine from a shell (I suspect it has modern SSL support linked in) so sometimes you want to do this without using requests or urllib2.
You can use the subprocess module to execute curl and get at the retrieved content:
import subprocess
// 'response' contains a []byte with the retrieved content.
// use '-s' to keep curl quiet while it does its job, but
// it's useful to omit that while you're still writing code
// so you know if curl is working
response = subprocess.check_output(['curl', '-s', baseURL % page_num])Python 3's subprocess module also contains .run() with a number of useful options. I'll leave it to someone who is actually running python 3 to provide that answer.
I use os library.
import os
os.system("sh script.sh")script.sh literally only contains the curl.
PYTHON 3
Only works within UNIX (Linux / Mac) (!)
Executing a cURL with Python 3 and parsing its JSON data.
import shlex
import json
import subprocess
# Make sure that cURL has Silent mode (--silent) activated
# otherwise we receive progress data inside err message later
cURL = r"""curl -X --silent POST -d 'username=test'"""
lCmd = shlex.split(cURL) # Splits cURL into an array
p = subprocess.Popen(lCmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate() # Get the output and the err message
json_data = json.loads(out.decode("utf-8"))
print(json_data) # Display now the dataSometimes you also need to install these dependencies on UNIX if you experience strange errors:
# Dependencies
sudo apt install libcurl4-openssl-dev libssl-dev
sudo apt install curl if you os supporting curl you can do something like this:
import os
os.system("curl -d @request.json --header "Content-Type: application/json" ")I'm using this way... And I think you can use this too!
by the way.. the module "os" is auto-installing when you install python. soo, you don't need to install packages ;)
0use requests lib.. this code is :
curl -LH "Accept: text/x-bibliography; style=apa" equal to this:
import requests
headers = { 'Accept': 'text/x-bibliography; style=apa',
}
r = requests.get(' headers=headers)
print(r.text) This could be achieve with the below mentioned psuedo code approach
Import os import requests Data = os.execute(curl URL) R= Data.json()
1