I wrote a python script to login to the management server read a csv file that contain a list of name, IP address and Color. But I keep getting that my payload is not Json. Please see my code below and the error not sure what else to do.
import requests, json, csv, base64
MGMT = '192.168.15.4'
url = 'https://192.168.15.4/web_api/v1.6.1/login'
payload= "{\r\n \"user\" : \"username\",\r\n \"password\" : \"*******\"\r\n}"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=False)
response_dict = json.loads(response.text)
session_id = response_dict['sid']
print(response_dict['sid'])
url = 'https://192.168.15.4/web_api/v1.6.1/publish'
payload="{ }"
headers = {
'Content-Type': 'application/json',
'X-chkp-sid': session_id
}
response = requests.request("POST", url, headers=headers, data=payload, verify=False)
#data = open("C:/Users/OdedinaP/Python_Learning/Add-Multi-Host/add_mutli_host.csv", 'rb')
with open("C:/Users/OdedinaP/Python_Learning/Add-Multi-Host/add_mutli_host.csv", 'r') as data:
csv_reader = csv.DictReader(data)
csv_data = list(csv_reader)
for index, item in enumerate(csv_data):
csvJson_data = {json.dumps(csv_data)}
for index, item in enumerate(csvJson_data):
payload = (csvJson_data)
headers = {
'Content-Type': 'application/json',
'X-chkp-sid': session_id
}
print(payload)
data = base64.encodebytes('payload'.encode())
print(data)
url = 'https://192.168.15.4/web_api/v1.6.1/add-host'
response = requests.request("POST", url, headers=headers, data=data, verify=False)
print(response.text)
Below is my output with the error:
$ python add_multi_host.py
C:\Users\OdedinaP\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host '192.168.15.4'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
warnings.warn(
C:\Users\OdedinaP\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host '192.168.15.4'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
warnings.warn(
C:\Users\OdedinaP\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host '192.168.15.4'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
warnings.warn(
atVmfEs8DGqoRaEREqHdiV9uVbI6O0KsJnisBDnDtOc
{'[{"name": "PC1", "ip-address": "192.168.10.1", "color": "Yellow"}, {"name": "PC2", "ip-address": "192.168.10.2", "color": "Yellow"}, {"name": "PC3", "ip-address": "192.168.10.3", "color": "Yellow"}, {"name": "PC4", "ip-address": "192.168.10.4", "color": "Yellow"}, {"name": "PC5", "ip-address": "192.168.10.5", "color": "Yellow"}, {"name": "PC6", "ip-address": "192.168.10.6", "color": "Yellow"}, {"name": "PC7", "ip-address": "192.168.10.7", "color": "Yellow"}, {"name": "PC8", "ip-address": "192.168.10.8", "color": "Yellow"}, {"name": "PC9", "ip-address": "192.168.10.9", "color": "Yellow"}, {"name": "PC10", "ip-address": "192.168.10.10", "color": "Yellow"}, {"name": "PC11", "ip-address": "192.168.10.11", "color": "Yellow"}, {"name": "PC12", "ip-address": "192.168.10.12", "color": "Yellow"}, {"name": "PC13", "ip-address": "192.168.10.13", "color": "Yellow"}, {"name": "PC14", "ip-address": "192.168.10.14", "color": "Yellow"}, {"name": "PC15", "ip-address": "192.168.10.15", "color": "Yellow"}, {"name": "PC16", "ip-address": "192.168.10.16", "color": "Yellow"}, {"name": "PC17", "ip-address": "192.168.10.17", "color": "Yellow"}, {"name": "PC18", "ip-address": "192.168.10.18", "color": "Yellow"}, {"name": "PC19", "ip-address": "192.168.10.19", "color": "Yellow"}, {"name": "PC20", "ip-address": "192.168.10.20", "color": "Yellow"}]'}
b'cGF5bG9hZA==\n'
{
"code" : "generic_err_invalid_syntax",
"message" : "Payload is not a valid JSON"
}