add a value to a json body request in python
Question:
I have created my own function which i import called timestamp, it returns two values :
from requests.auth import HTTPBasicAuth
import requests
import json
def timeframe():
response = requests.get("https://$host/api/profiler/1.13/reporting/timestamps.json", verify=False, auth=HTTPBasicAuth("admin", "admin"))
time = response.json()
for entry in time:
if entry.get('data_resolution') == 'min':
if entry.get('datasource') == 'FDS_TRAFFIC':
start_time = entry['start_time']
end_time = entry['end_time']
return start_time, end_time
timeframe()
i need to add timestamps to a keys in a json body request, you will see ‘end’ & ‘start’ keys. I need to retrieve those timestamps and somehow add them to those keys.
import requests
import timestamp
from requests.auth import HTTPBasicAuth
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
stamp = timestamp.timeframe()
print(stamp)
url = 'http://10.65.170.112/api/profiler/1.12/reporting/reports'
headers = {'Content-Type': 'application/json'}
payload = {
"criteria": {
"time_frame": {
"start": str(stamp[0]),
"end": str(stamp[1]),
"resolution": "flow"
},
"query": {
"realm": "traffic_flow_list",
"sort_column": 41,
"devices": [
{
"ipaddr": "10.65.170.2"
}
],
"group_by": "flw",
"columns": [
729,
40,
41,
14,
44,
10,
45,
46
]
}
},
"template_id": 184
}
req = requests.post(url, headers=headers, data = payload, verify=False, auth=HTTPBasicAuth('admin', 'admin'),)
print(req.status_code, req.text)
Not sure what to do.
Thanks
Answers:
The function you created returns a tuple: return start_time, end_time
.
So, a way to implement would be:
start, end = timestamp.timeframe()
Then, you can hydrate your body:
body = {
"criteria": {
"time_frame": {
"end": end,
"start": start,
"resolution": "flow"
},
import requests
import timestamp
from requests.auth import HTTPBasicAuth
import urllib3
import json
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
stamp = timestamp.timeframe()
auth = HTTPBasicAuth("admin", "admin")
url = "https://$host/api/profiler/1.13/reporting/reports"
headers = {'Content-Type': 'application/json'}
payload = {
"criteria": {
"time_frame": {
"start": stamp[0],
"end": stamp[1],
"resolution": "flow"
},
"query": {
"realm": "traffic_summary",
"sort_column": 41,
"devices": [{ "ipaddr": "10.65.170.2"}],
"group_by": "hos",
"columns": [
729,
40,
41,
14,
44,
10,
45,
46
]
}
},
"template_id": 184
}
req = requests.post(url, verify=False, auth=auth, headers=headers, data=json.dumps(payload))
print(req.headers)
Was resolved by adding data=json.dump(payload)
I have created my own function which i import called timestamp, it returns two values :
from requests.auth import HTTPBasicAuth
import requests
import json
def timeframe():
response = requests.get("https://$host/api/profiler/1.13/reporting/timestamps.json", verify=False, auth=HTTPBasicAuth("admin", "admin"))
time = response.json()
for entry in time:
if entry.get('data_resolution') == 'min':
if entry.get('datasource') == 'FDS_TRAFFIC':
start_time = entry['start_time']
end_time = entry['end_time']
return start_time, end_time
timeframe()
i need to add timestamps to a keys in a json body request, you will see ‘end’ & ‘start’ keys. I need to retrieve those timestamps and somehow add them to those keys.
import requests
import timestamp
from requests.auth import HTTPBasicAuth
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
stamp = timestamp.timeframe()
print(stamp)
url = 'http://10.65.170.112/api/profiler/1.12/reporting/reports'
headers = {'Content-Type': 'application/json'}
payload = {
"criteria": {
"time_frame": {
"start": str(stamp[0]),
"end": str(stamp[1]),
"resolution": "flow"
},
"query": {
"realm": "traffic_flow_list",
"sort_column": 41,
"devices": [
{
"ipaddr": "10.65.170.2"
}
],
"group_by": "flw",
"columns": [
729,
40,
41,
14,
44,
10,
45,
46
]
}
},
"template_id": 184
}
req = requests.post(url, headers=headers, data = payload, verify=False, auth=HTTPBasicAuth('admin', 'admin'),)
print(req.status_code, req.text)
Not sure what to do.
Thanks
The function you created returns a tuple: return start_time, end_time
.
So, a way to implement would be:
start, end = timestamp.timeframe()
Then, you can hydrate your body:
body = {
"criteria": {
"time_frame": {
"end": end,
"start": start,
"resolution": "flow"
},
import requests
import timestamp
from requests.auth import HTTPBasicAuth
import urllib3
import json
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
stamp = timestamp.timeframe()
auth = HTTPBasicAuth("admin", "admin")
url = "https://$host/api/profiler/1.13/reporting/reports"
headers = {'Content-Type': 'application/json'}
payload = {
"criteria": {
"time_frame": {
"start": stamp[0],
"end": stamp[1],
"resolution": "flow"
},
"query": {
"realm": "traffic_summary",
"sort_column": 41,
"devices": [{ "ipaddr": "10.65.170.2"}],
"group_by": "hos",
"columns": [
729,
40,
41,
14,
44,
10,
45,
46
]
}
},
"template_id": 184
}
req = requests.post(url, verify=False, auth=auth, headers=headers, data=json.dumps(payload))
print(req.headers)
Was resolved by adding data=json.dump(payload)