So, my idea at the first point, was to execute a cURL after a given condition in Python.
I tryed to use the os module, using:
os.system(curl -k "http://myhost.com" -d'{"event": { "Error": "Error Found", "Host": "myhostname"}}')
But I had a syntax error.
I know it is hardcoded, and also that there is a module called requests in Python, so..
My doubt is, can I just set this full link as a variable? like
mycurl = curl -k "http://myhost.com" -d'{"event": { "Error": "Error Found", "Host": "myhostname"}}'
then execute:
os.system(mycurl)
If it's not possible, or it's incorrect, then how can I use the module requests in this case? Because I want to set an Authorization Token with this curl, which is:
curl -k "http://myhost.com" -H "Authorization: MYTOKEN" -d'{"event": { "Error": "Error Found", "Host": "myhostname"}}'
Here is how you might do it via requests:
import requests
# ...
if i in str(read_log):
url = "http://myhost.com"
headers = {
"Authorization": "MYTOKEN"
}
payload = {
"event": {
"Error": "Error Found",
"Host": "myhostname"
}
}
response = requests.post(url, headers=headers, json=payload)
# Do something with response...
Like Barmar above said, the argument to os.system needs to be a string. You can escape quotes " in strings with a backslash \. For example:
os.system("curl -k \"http://myhost.com\" -d'{\"event\": { \"Error\": \"Error Found\", \"Host\": \"myhostname\"}}'")
Just to make the os.system approach a bit clearer, you can combine f-strings and docstrings:
URL = "http://myhost.com"
TOKEN = MYTOKEN
MY_HOSTNAME = myhostname
curl_command = f"""curl -k {URL}
-H "Authorization: {TOKEN}"
-d'{"event": { "Error": "Error Found", "Host": {MY_HOSTNAME}}}'
"""
os.system(curl_command)
Related
If I run a curl statement in bash I get the result I want with no problem.
curl --header 'Content-Type: application/json' --header 'Authorization: Token ABC123' --data '{"jsonrpc":"2.0","method":"enterprise/getEnterpriseEdges","params":{"with":["certificates","configuration","links","recentLinks","site","licenses","analyticsMode","selfHealing"],"edgeIds":[4723]},"id":34123423}' --request POST 'https://my-portal.velocloud.net/portal/'
When putting it into a python script, I keep getting this result instead of the massive JSON array of data.
{'jsonrpc': '2.0', 'error': {'code': -32603, 'message': 'JSON parse error'}, 'id': 1}
This is a copy of my Python script
import requests
payload_headers = {
"Content-Type": "application/json",
"Authorization": "Token ABC123"
}
payload_data = {
"jsonrpc": "2.0",
"method": "enterprise/getEnterpriseEdges",
"params": {
"with": ["certificates", "configuration", "links", "recentLinks", "site", "licenses", "analyticsMode", "selfHealing"],
"edgeIds": [4723]
},
"id": 34
}
response = requests.post("https://my-portal.velocloud.net/portal/", headers=payload_headers, data=payload_data, verify= False)
json_info = response.json()
print(json_info)
I was hoping to get a JSON output of the data like I get with the curl in bash, and eventually incorporate a loop to run through a list to get multiple JSONs to build out an API.
You need to dump the dict to str:
import json
response = requests.post("https://my-portal.velocloud.net/portal/",
headers=payload_headers,
data=json.dumps(payload_data),
verify=False)
Or just use json parameter:
response = requests.post("https://my-portal.velocloud.net/portal/",
headers=payload_headers,
json=payload_data,
verify=False)
I am trying to execute the following cURL command using python script. I want to than save the results as text file.
cURL command:
curl -d #myfile.json -H "Content-Type: application/json" -i "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxx"
Python Script:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxx"
payload = {r"C:\Users\Desktop\myfile.json"}
res = requests.post(url, data=payload)
print(res.text)
Error:
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unexpected token.\myfile.json\n^",
"errors": [
{
"message": "Invalid JSON payload received. Unexpected token.\myfile.json\n^",
"domain": "global",
"reason": "parseError"
}
],
"status": "INVALID_ARGUMENT"
}
}
From the error, i can understand that the way i am providing the json file must be wrong. I tried different ways but error still remains. The json file contain set of parameters required for the API call.
please could any guide here. Also indicate how to save the output as text.
It's close but all you're doing is sending the json file name to the remote HTTP server. You need to open the file first:
import requests
from requests.structures import CaseInsensitiveDict
url = "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxx"
with open("C:\Users\Desktop\myfile.json", 'rb') as payload:
res = requests.post(url, data=payload)
print(res.text)
I have the following curl command which works fine:
curl -X POST -H 'content-type: application/json' -H "DD-API-KEY: ${api_key}" -H "DD-APPLICATION-KEY: ${app_key}" \
-d '{
"query": "service:my_service",
"time": {
"from": "2019-11-28T00:00:00Z",
"to": "2019-11-28T16:00:00Z"
},
"sort": "asc",
"limit": 1000
}' "https://api.datadoghq.com/api/v1/logs-queries/list" -o output3.json5
Then I convert this requests to Python Requests, and the curl method works but Python returns a 500 error without any details.
import requests
def main():
headers = {
'content-type': 'application/json',
'DD-API-KEY': 'AAA',
'DD-APPLICATION-KEY': 'XXX',
}
data = {
"query": "service:my_service",
"time": {
"from": "now - 1h",
"to": "now"
},
"sort": "asc",
"limit": 50
}
response=requests.post("https://api.datadoghq.com/api/v1/logs-queries/list",headers=headers, data=data)
I tried it outside my Docker guessing that maybe connection was the key, but it doesn't work either.
Point both of those at a service like httpbin to see how they differ.
Requests' data option for POST requests generates form-encoded data by default, while curl passes the JSON string through directly. You can manually encode your payload as a JSON string:
import json
response = requests.post(..., data=json.dumps(data))
# ^^^^^^^^^^
or if you have Requests version 2.4.2 or later you can use the json parameter to have your dict converted to JSON automatically:
response = requests.post(..., json=data)
# ^^^^
I'm trying to get report data through Apple Search Ads API. So I use the method
-H ...\
-d "#TestSearchTermReport.json"
-X POST "/v1/reports/campaigns/{cId}/searchterms"
I have included all the headers and credentials. The following is the content of my json data file for the POST body:
{
"startTime": "2016-11-13",
"endTime": "2016-11-13",
"timezone": "UTC",
"granularity": "DAILY",
"selector": {
"orderBy":[{"field":"spend","sortOrder":"DESCENDING"}],
"fields": ["spend", "taps", "conversions", "avgCPA", "avgCPC", "ttr", "conversionRate"],
"pagination": {
"offset": 0,
"limit": 1000
}
},
"groupBy": "countryCode",
"returnRowTotals": False,
"returnRecordsWithNoMetrics": False
}
However, I get the following error message:
{"data":null,"pagination":null,"error":{"errors":[{"messageCode":"INVALID_JSON_REQUEST","message":"This is an invalid json. The request can not be parsed","field":"Line#:1 Column#:3"}]}}
I have tried many times through different ways, but still not working. Is there any smart guys can help me?
Thanks in advance!
I've just been struggling with this API myself, the documentation is not exactly user friendly!
Looks like you have a few issues here:
Timezone and Granularity are enums, so their values need to be numeric, not strings. I'm actually still getting an error every time I call with the timezone field, so have omitted this for the time being, until I can find a solution.
Some of your field names are incorrect; spend, avgCPC and countryCode should be localSpend, avgCPT and COUNTRY_CODE, respectively.
The group by field should be a list.
As you're using python, try this:
import requests
org_id = <YOUR_ORG_ID>
certificate_path = '<PATH_TO_YOUR_CERTIFICATE>'
certificate_key_path = '<PATH_TO_YOUR_CERTIFICATE_KEY>'
campaign_id = <YOUR_CAMPAIGN_ID>
headers = {"Authorization": "orgId=%s" % org_id}
payload = {
"startTime": "2016-11-13",
"endTime": "2016-11-13",
"granularity": 1,
"selector": {
"orderBy":[{"field":"localSpend","sortOrder":"DESCENDING"}],
"fields": ["localSpend", "taps", "conversions", "avgCPA", "avgCPT", "ttr", "conversionRate"],
"pagination": { "offset": 0, "limit": 1000 }
},
"groupBy": ["COUNTRY_CODE"],
"returnRowTotals": False,
"returnRecordsWithNoMetrics": False
}
url = 'https://api.searchads.apple.com/api/v1/reports/campaigns/%s/searchterms' % campaign_id
response = requests.post(url, cert=(certificate_path, certificate_key_path), json=payload, headers=headers)
print(response.text)
This returns a successful response for me. Hope it works for you too!
I was able to get it working using the following curl
curl --cert ./<PI2 CERTIFICATE FILE>.p12 --pass <PI2 CERTIFICATE PASSWORD> -H "Authorization: orgId=xxx" -H "Content-Type: application/json" -X POST -d ' {"startTime": "2017-04-06", "endTime": "2017-04-06", "granularity": 2, "selector": {"orderBy":[{"field":"localSpend","sortOrder":"DESCENDING"}], "fields": ["localSpend"], "pagination": { "offset": 0, "limit": 1000 } }, "groupBy": ["COUNTRY_CODE"], "returnRowTotals": false, "returnRecordsWithNoMetrics": false }' "https://api.searchads.apple.com/api/v1/reports/campaigns/campaign name/searchterms"
You can obtain the p12 certificate by following the steps mentioned here
https://developer.apple.com/library/content/documentation/General/Conceptual/AppStoreSearchAdsAPIReference/API_Overview.html#//apple_ref/doc/uid/TP40017495-CH7-SW8
If you are using requests in python to make the post call you might have to do some extra work as I did not find any parameters that takes in p12 certificate and password as input. Create a crt file and pem using openssl
openssl pkcs12 -in Apple_Certificate.p12 -out file.crt.pem -clcerts -nokeys
openssl pkcs12 -in Apple_Certificate.p12 -out file.key.pem -nocerts -nodes
and use the following code
headers = {
'Authorization': 'orgId=<ORG_ID>',
'Content-Type': 'application/json',
}
data = ' {"startTime": "%s",
"endTime": "%s",
"granularity": 2, ' \
'"selector": {"orderBy":[{"field":"localSpend","sortOrder":"DESCENDING"}], ' \
'"fields": ["localSpend"], "pagination": { "offset": 0, "limit": 1000 } }, ' \
'"groupBy": ["COUNTRY_CODE"], "returnRowTotals": false, "returnRecordsWithNoMetrics": false }' % (date_report, date_report)
url = 'https://api.searchads.apple.com/api/v1/reports/campaigns/%s/searchterms' % (your_campaign_id)
r = requests.post(url, headers=headers, data=data,
cert=('<path to crt file>',
'<path to key file>'))
You can use the postman:
Steps:
1. Import Row text from the Postman -
curl -X GET 'https://api.searchads.apple.com/api/v2/campaigns/124324'
-H 'Authorization: orgId=234234' -H 'Content-Type: application/json'
Set certificate and key in Postman settings (My configuration for macOS 😊) -
Now you can use the request at postman -
I am writing a python script which will call a REST POST endpoint but in response I am getting 400 Bad Request where as if I do same request with curl, it returns me 200 OK. Code snippet for python script is below
import httplib,urllib
def printText(txt):
lines = txt.split('\n')
for line in lines:
print line.strip()
httpServ = httplib.HTTPConnection("127.0.0.1", 9100)
httpServ.connect()
params = urllib.urlencode({"externalId": "801411","name": "RD Core","description": "Tenant create","subscriptionType": "MINIMAL","features": {"capture":False,"correspondence": True,"vault": False}})
headers = {"Content-type": "application/json"}
httpServ.request("POST", "/tenants", params, headers)
response = httpServ.getresponse()
print response.status, response.reason
httpServ.close()
and corresponding curl request is
curl -iX POST \
-H 'Content-Type: application/json' \
-d '
{
"externalId": "801411",
"name": "RD Core seed data test",
"description": "Tenant for Core team seed data testing",
"subscriptionType": "MINIMAL",
"features": {
"capture": false,
"correspondence": true,
"vault": false
}
}' http://localhost:9100/tenants/
Now I am not able figure out where is the issue in python script.
Try using requests (install with pip install requests) instead of urllib.
Also, enclose your data as JSON in the request body, don't pass them as URL parameters. You are passing JSON data in your curl example as well.
import requests
data = {
"externalId": "801411",
"name": "RD Core",
"description": "Tenant create",
"subscriptionType": "MINIMAL",
"features": {
"capture": False,
"correspondence": True,
"vault": False
}
}
response = requests.post(
url="http://localhost:9100/tenants/",
json=data
)
print response.status_code, response.reason
EDIT
From https://2.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests:
Note, the json parameter is ignored if either data or files is passed.
Using the json parameter in the request will change the Content-Type
in the header to application/json.
The problem in your code is, you set Content-Type header as application/json and not sending data in json format
import httplib, json
httpServ = httplib.HTTPConnection("127.0.0.1", 9100)
httpServ.connect()
headers = {"Content-type": "application/json"}
data = json.dumps({
"externalId": "801411",
"name": "RD Core",
"description": "Tenant create",
"subscriptionType": "MINIMAL",
"features": {
"capture": False,
"correspondence": True,
"vault": False
}
})
# here raw data is in json format
httpServ.request("POST", "/tenants", data, headers)
response = httpServ.getresponse()
print response.status, response.reason
httpServ.close()