add a value to a json body request in python - python

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)

Related

Concatenate Items per Page from Json Path in a dataframe

I have a JSON Path avaliable by Dock API https://lighthouse.dock.tech/docs/cards-and-digital-banking-api-reference/1403b37717e98-list-pix-infractions and its have one limitation of 20 rows or registers per page - MaxItemsPerPage = 20- but i have more than 1000 items - totalItems = 1050.
{
"previousPage": 0,
"currentPage": 0,
"nextPage": 1,
"last": false,
"totalPages": 1,
"totalItems": 1050,
"maxItemsPerPage": 20,
"totalItemsPage": 1,
"items": [
{
"status": "OPEN",
"creditedParticipant": "08706265",
"infractionType": "FRAUD",
"reportedBy": "DEBITED_PARTICIPANT",
"lastModified": "2020-01-17T10:01:00Z",
"debitedParticipant": "99999010",
"creationTime": "2020-01-17T10:00:00Z",
"endToEndId": "E9999901012341234123412345678900",
"reportDetails": "Details that can help the receiving participant to analyze the id",
"responseTime": "2020-01-17T11:00:00Z",
"analysisResult": "AGREED",
"id": "91d65e98-97c0-4b0f-b577-73625da1f9fc",
"correlationId": "evp",
"analysisDetails": "Details of the infraction analysis"
}
]
}
how can i concatenate a set of registers in a multiples pages by period? I have this in python:
import http.client
conn = http.client.HTTPSConnection("pix-baas.caradhras.io")
headers = {
'Content-Type': "application/json",
'Authorization': "334jh89d0"
}
conn.request("GET", "/pix-infractions/v1/list?page=1&from=2020-01-17T10%3A01%3A00Zv&to=2022-01-17T10%3A01%3A00Z", headers=headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

JSONDecodeError: Expecting ',' delimiter in a long JSON string

i'm trying to parse the following JSON but I always face the error stating "JSONDecodeError: Expecting ',' delimiter"
Here is the code i'm doing:
import requests
from bs4 import BeautifulSoup
import json
page_link="https://www.indeed.com/cmp/Ocean-Beauty-Seafoods/reviews?start=0"
page_response = requests.get(page_link, verify=False)
soup = BeautifulSoup(page_response.content, 'html.parser')
strJson=soup.findAll('script')[16].text.replace("\n window._initialData=JSON.parse(\'","").replace("');","")
json.loads(strJson)
manyy thanks
The Json as it is isn't valid. Try to "preprocess" it first with ast.literal_eval:
import json
import requests
from ast import literal_eval
from bs4 import BeautifulSoup
page_link = "https://www.indeed.com/cmp/Ocean-Beauty-Seafoods/reviews?start=0"
page_response = requests.get(page_link, verify=False)
soup = BeautifulSoup(page_response.content, "html.parser")
strJson = (
soup.findAll("script")[16]
.text.replace("\n window._initialData=JSON.parse('", "")
.replace("');", "")
)
s = literal_eval("'''" + strJson + "'''")
data = json.loads(s)
print(json.dumps(data, indent=4))
Prints:
{
"breadcrumbs": {
"breadcrumbs": [
{
"name": "Companies",
"noFollow": false,
"url": "https://www.indeed.com/companies"
},
{
"name": "Ocean Beauty Seafoods",
"noFollow": false,
"url": "https://www.indeed.com/cmp/Ocean-Beauty-Seafoods"
},
{
"name": "Employee Reviews",
"noFollow": false
}
]
},
"companyPageFooter": {
"enabledToShowUserFeedbackForm": false,
"encodedFccId": "a9c95405fb0cdb1c",
"stickyJobsTabLink": {
"jobsLink": "/cmp/Ocean-Beauty-Seafoods/jobs"
}
},
"companyPageHeader": {
"auroraLogoUrl": "https://d2q79iu7y748jz.cloudfront.net/s/_squarelogo/64x64/147cafc3914ffb4693dc99df6ad0b169",
"auroraLogoUrl2x": "https://d2q79iu7y748jz.cloudfront.net/s/_squarelogo/128x128/147cafc3914ffb4693dc99df6ad0b169",
"brandColor": "#FFFFFF",
"companyHeader": {
"name": "Ocean Beauty Seafoods",
"rating": 3.7,
"reviewCount": 114,
"reviewCountFormatted": "114",
"reviewsUrl": "/cmp/Ocean-Beauty-Seafoods/reviews"
},
...and so on.

Python to parse nested JSON values that can be null sometimes

I'm trying to parse the following and pull out primary_ip as a variable. Sometimes primary_ip is "null". Here is an example of the JSON, code and the most recent error I am getting.
{
"count": 67,
"next": "https://master.netbox.dev/api/dcim/devices/?limit=50&offset=50",
"previous": null,
"results": [
{
"id": 28,
"url": "https://master.netbox.dev/api/dcim/devices/28/",
"name": "q2",
"display_name": "q2",
"device_type": {
"id": 20,
"url": "https://master.netbox.dev/api/dcim/device-types/20/",
"manufacturer": {
"id": 15,
"url": "https://master.netbox.dev/api/dcim/manufacturers/15/",
"name": "Zyxel",
"slug": "zyxel"
},
"model": "GS1900",
"slug": "gs1900",
"display_name": "Zyxel GS1900"
},
"device_role": {
"id": 4,
"url": "https://master.netbox.dev/api/dcim/device-roles/4/",
"name": "Access Switch",
"slug": "access-switch"
},
"primary_ip": {
"id": 301,
"url": "https://master.netbox.dev/api/ipam/ip-addresses/301/",
"family": 4,
"address": "172.31.254.241/24"
},
Example Python
import requests
import json
headers = {
'Authorization': 'Token 63d421a5f733dd2c5070083e80df8b4d466ae525',
'Accept': 'application/json; indent=4',
}
response = requests.get('https://master.netbox.dev/api/dcim/sites/', headers=headers)
j = response.json()
for results in j['results']:
x=results.get('name')
y=results.get('physical_address')
response2 = requests.get('https://master.netbox.dev/api/dcim/devices', headers=headers)
device = response2.json()
for result in device['results']:
x=result.get('name')
z=result.get('site')['name']
# if result.get('primary_ip') != None
y=result.get('primary_ip', {}).get('address')
print(x,y,z)
I get the following error when I run it:
ubuntu#ip-172-31-39-26:~$ python3 Netbox-python
Traceback (most recent call last):
File "Netbox-python", line 22, in <module>
y=result.get('primary_ip', {}).get('address')
AttributeError: 'NoneType' object has no attribute 'get'
Which value is None? Is it the primary_ip or is it address ?
you could try the following:
y = result.get('primary_ip', {}).get('address, 'empty_address')
This will replace any None values with empty_address
Update:
I have just ran your code and got the following output:
LC1 123.123.123.123/24 site1
q1 172.31.254.254/24 COD
q2 172.31.254.241/24 COD
After running this:
import requests
import json
headers = {
"Authorization": "Token 63d421a5f733dd2c5070083e80df8b4d466ae525",
"Accept": "application/json; indent=4",
}
response = requests.get("https://master.netbox.dev/api/dcim/sites/", headers=headers)
j = response.json()
for results in j["results"]:
x = results.get("name")
y = results.get("physical_address")
response2 = requests.get("https://master.netbox.dev/api/dcim/devices", headers=headers)
device = response2.json()
for result in device["results"]:
x = result.get("name")
z = result.get("site")["name"]
if result.get("primary_ip") != None:
y = result.get("primary_ip").get("address")
print(x, y, z)
I am not sure of the expected output but the code doesn't throw any errors. From looking at the code, it seems there were a few indentation errors, where things didn't match up in terms of where they should have been indented.

add params to url in python

I would like to pass two parameters to my url(status code & parent id). The json response of the url request is such :
{
"page": 1,
"per_page": 10,
"total": 35,
"total_pages": 4,
"data": [
{
"id": 11,
"timestamp": 1565193225660,
"status": "RUNNING",
"operatingParams": {
"rotorSpeed": 2363,
"slack": 63.07,
"rootThreshold": 0
},
"asset": {
"id": 4,
"alias": "Secondary Rotor"
},
"parent": {
"id": 2,
"alias": "Main Rotor Shaft"
}
}]
I would like to know how to pass the two parameters in the url. Passing ?status=RUNNING gives the response of all the devices which have running as status (thats pretty straightforward).
For now I have tried this:
import requests
resp = requests.get('https://jsonmock.hackerrank.com/api/iot_devices/search?status=RUNNING')
q = resp.json()
print(q)
How should I pass in parentid=2, so it returns a response with devices which have their parent id=2.Thank you.
It's plainly documented under "Passing Parameters in URLs" in the Requests docs.
resp = requests.get(
'https://jsonmock.hackerrank.com/api/iot_devices/search',
params={
'status': 'RUNNING',
'parentid': 2,
},
)
To add a second get parameter, use the & separator :
import requests
resp = requests.get('https://jsonmock.hackerrank.com/api/iot_devices/search?status=RUNNING&parentid=2')
q = resp.json()
print(q)
If you want to send data via get request the process is straight forward note how different values are seperated with '&'.
url?name1=value1&name2=value2
If you are using Flask for backend then you can access these parameters like.
para1=request.args.get("name1")
para2=request.args.get("name2")
On the front end you can use ajax to send the request
var xhttp=new XMLHttpRequest();
var url="url?name1=value1&name2=value2"
xhttp.open("GET",url,true)
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200)
{
console.log(this.responseText);
}
};
xhttp.send();

POST CSV to server using python

I an trying to Post CSV using python request. I am using the following code but getting this error:
{"code":400,"message":"Invalid input parameters","status":"error"}
Here is my code:
import requests
import json
api_url = "https://anlyticstts.com//api/insights/v1/reports"
headers = {
'Content-Type': 'application/json',
'X-access-key': 'e13168e9f1504d63455'
}
data = {
"search_term_ids": [60, 61],
"product_list_ids": [120],
"start_date": "20180801",
"end_date": "20180805",
"columns": {
"product": ["crawl_date", "product_name"],
"status": ["no_longer_available"],
"ranking": ["search_rank"],
"pricing": ["price"]},
"page_one_only": True, "format": "csv"
}
r = requests.post(url=api_url, data=data, headers=headers)
print(r.text)
You can try this sample code
import json
r = requests.post(api_url, data=json.dumps(data), headers=headers)
instead of
r = requests.post(url=api_url, data=data, headers=headers)

Categories