How to use curl command via python - python

I want to send a json file to keen.io in their documentation they use the following command
curl "https://api.keen.io/3.0/projects/PROJECT_ID/events/EVENT_COLLECTION?api_key=WRITE_KEY" -H "Content-Type: application/json" -d #purchase1.json
I am thinking how can I use this to work with python

you can use subprocess module to run the curl command using subprocess.call() method.
eg :
subprocess.call('curl "https://api.keen.io/3.0/projects/PROJECT_ID/events/EVENT_COLLECTION?api_key=WRITE_KEY" -H "Content-Type: application/json" -d #purchase1.json', shell=true)

You could use the requests library to achieve this.
import requests
uri = "https://api.keen.io/3.0/projects/{}/events/{}?api_key={}".format(PROJECT_ID, EVENT_COLLECTION, API_KEY)
json_payload = open('purchase1.json', 'rb').read()
requests.post(uri, json=json_payload)
You can read through their documentation for more information.

You can also use subprocess to execute the command and then catch the output https://docs.python.org/2/library/subprocess.html

Related

How to send '-X POST' using 'requests' library in Python?

I am trying to replicate shell command:
curl -u [staff_email]:[api_key] -F "case[attachments][0]=#/path/to/file1.ext" -F "case[attachments][1]=#/path/to/file2.ext" -F "case[content]=I need help" -F "case[subject]=I need help" -F "case[user_email]=user#domain.ru" -F "case[user_full_name]=FullName" -F "case[language_id]=2" -F "case[custom_fields][cf_44]=3" -X POST https://[domain].omnidesk.ru/api/cases.json
into Python code using Requests library:
import requests
What is a proper syntax for this example (curl -X POST)?
You can use requests.post, as explained here
for the attachments part (your first -F), read here
for the auth part (-u): here

python - convert curl command

I have this curl command.
This works
curl -O -H "X-Api:3433432" -X GET "https://website.com/file.zip"
Trying to figure it out how to convert it to something python understands.
curl -O -H "X-Api:3433432" -X GET "https://website/file.zip"
Perhaps you're looking for the "Requests" module?
It allows you to create a GET request in a similar way you would use a curl command.
You can find some documentation at:
https://www.w3schools.com/PYTHON/ref_requests_get.asp
Edit: There's also https://curlconverter.com/ which should convert it automatically for you.
try using module requests:
import requests
headers = {
'X-Api': '3433432',
}
response = requests.get('https://website.com/file.zip', headers=headers)

Transformaing CURL post request to python request

I am running a CURL command and want to update it to python request command.
CURL Command:
curl -H "Content-Type: application/json" -X POST -k -s --date '{"command": "/semate:severitytoAll', "target": "/gen1/gateway[(#name=\"#gname\")]/directory/probe[(#name=\"#probn\")]/managedE[(#name=\"#me\")]/sam[#name=\"PRO")] [(#type=\"\')]/dav[(#name=\"PRO\")]/row/rows[(#name=\""PRO_D\')]", "args': {"1": "testing sn via Rest aPI", '2": 1, "4":3}}' -u #gatName
Can someone guide me with the equivalent python request commands please
Regards
Bicky
check https://www.postman.com/product/rest-client/
Write it in Postman, select code and select Python.

How to execute curl command using python 3.6 windows

I have a curl command, Which works on my UNIX server.I want to execute it using python script.
curl comma:
curl -X POST -H "Content-type: application/vnd.appd.cntrl+json;v=1" -d '{"name":"Suppression_Testing","timeRange": {"startTimeMillis": "2017-12-25T04:16:30+0000","endTimeMillis": "2017-12-26T06:16:30+0000"},"affects": {"type": "APP"}}' --user api#adgm-nonprod:ac#123
https://apcn.adm.com/controller/api/accounts/3/applications/61/actionsuppressions
I tried import os and executed curl butI am getting error as POST invalid syntax.
Also I tried using subprocess
subprocess.Popen(curl 0) but no luck. Can anyone help me?
Many Thanks,
Apurva Acharya
you can use python , subprocess module to do this
from subprocess import Popen
command='''curl -X POST -H "Content-type: application/vnd.appd.cntrl+json;v=1" -d '{"name":"Suppression_Testing","timeRange": {"startTimeMillis": "2017-12-25T04:16:30+0000","endTimeMillis": "2017-12-26T06:16:30+0000"},"affects": {"type": "APP"}}' --user api#adgm-nonprod:ac#123'''
#use shell=True , this will allow you to run the command in a single string on a shell like environment
proc=Popen(command,shell=True)

python script to run dynamic curl request

Python newb here, here's my current script:
#!/usr/bin/env python
import os
import time
import datetime
import subprocess
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime(%Y%m%d)
My curl command:
curl -i -k -H -o <timestamp>.txt "Accept: application/json" -H "Content-Type: application/json" -H "appID:******" -H "appKey:*******" -X GET https://*****.com/csOpen/workplace/hr/v1/employees_s?type=EMP&location=******&term_from=<timestamp>
The dynamic aspect of this curl request comes from the python portion of my script. I need the output file to be the $currentTime.txt and i need the php variable $term_from to also be the timestamp.
So far ive tried invoking the curl command using
os.system('curl -i -k -H -o' + %st + '.txt "Accept: application/json" -H "Content-Type: application/json" -H "appID:arcsght_app" -H "appKey:*****" -X GET https://csopen.teamaol.com/csOpen/workplace/hr/v1/employees_s?type=EMP&location=Dulles&term_from=' + %st)
That didn't work, then i tried using
subprocess.call(<same curl command as above>)
and that didnt work.
Ive tried my curl command from bash and it works, and i can get my timestamp to show how i need it. I just cant figure out how to tie everything together. Before i posted this i did try to figure it out on my own, but this is my first real adventure into python so my knowledge of what works and what doesn't is pretty slim. Looking for help! Thanks.
my_command = 'curl -i -k -H -o {timestamp}.txt "Accept: application/json" -H "Content-Type: application/json" -H "appID:******" -H "appKey:*******" -X GET https://*****.com/csOpen/workplace/hr/v1/employees_s?type=EMP&location=******&term_from={timestamp}'.format(timestamp=123456)
os.system(my_command)
should work fine ... Im not entirely sure why you want to do this in python... but this should allow you to no problem

Categories