<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Problem with Python Script and Post Request in API / CLI Discussion</title>
    <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132565#M6368</link>
    <description>&lt;P&gt;First of all, you realize you don't even need to write a Python script for this, right?&lt;BR /&gt;You can import using mgmt_cli and a specially crafted CSV.&amp;nbsp;&lt;BR /&gt;There are several examples in the community, but here's one recent discussion:&amp;nbsp;&lt;A href="https://community.checkpoint.com/t5/API-CLI-Discussion/MGMT-CLI-Object-import/m-p/108135#M5494" target="_blank"&gt;https://community.checkpoint.com/t5/API-CLI-Discussion/MGMT-CLI-Object-import/m-p/108135#M5494&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To your script: it's not clear to me if you're actually looping through the items and submitting each one individually to add-host or if you're trying to submit them all at once with a single call.&lt;BR /&gt;The output suggests you're trying to do the latter, which definitely work work.&lt;/P&gt;</description>
    <pubDate>Mon, 25 Oct 2021 19:14:34 GMT</pubDate>
    <dc:creator>PhoneBoy</dc:creator>
    <dc:date>2021-10-25T19:14:34Z</dc:date>
    <item>
      <title>Problem with Python Script and Post Request</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132561#M6366</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import requests, json, csv, base64&lt;BR /&gt;MGMT = '192.168.15.4'&lt;BR /&gt;url = '&lt;A href="https://192.168.15.4/web_api/v1.6.1/login" target="_blank"&gt;https://192.168.15.4/web_api/v1.6.1/login&lt;/A&gt;'&lt;BR /&gt;payload= "{\r\n \"user\" : \"username\",\r\n \"password\" : \"*******\"\r\n}"&lt;BR /&gt;headers = {&lt;BR /&gt;'Content-Type': 'application/json'&lt;BR /&gt;}&lt;BR /&gt;response = requests.request("POST", url, headers=headers, data=payload, verify=False)&lt;BR /&gt;response_dict = json.loads(response.text)&lt;BR /&gt;session_id = response_dict['sid']&lt;BR /&gt;print(response_dict['sid'])&lt;/P&gt;&lt;P&gt;url = '&lt;A href="https://192.168.15.4/web_api/v1.6.1/publish" target="_blank"&gt;https://192.168.15.4/web_api/v1.6.1/publish&lt;/A&gt;'&lt;BR /&gt;payload="{ }"&lt;BR /&gt;headers = {&lt;BR /&gt;'Content-Type': 'application/json',&lt;BR /&gt;'X-chkp-sid': session_id&lt;BR /&gt;}&lt;BR /&gt;response = requests.request("POST", url, headers=headers, data=payload, verify=False)&lt;BR /&gt;#data = open("C:/Users/OdedinaP/Python_Learning/Add-Multi-Host/add_mutli_host.csv", 'rb')&lt;BR /&gt;with open("C:/Users/OdedinaP/Python_Learning/Add-Multi-Host/add_mutli_host.csv", 'r') as data:&lt;BR /&gt;csv_reader = csv.DictReader(data)&lt;BR /&gt;csv_data = list(csv_reader)&lt;BR /&gt;for index, item in enumerate(csv_data):&lt;BR /&gt;csvJson_data = {json.dumps(csv_data)}&lt;BR /&gt;for index, item in enumerate(csvJson_data):&lt;BR /&gt;payload = (csvJson_data)&lt;BR /&gt;headers = {&lt;BR /&gt;'Content-Type': 'application/json',&lt;BR /&gt;'X-chkp-sid': session_id&lt;BR /&gt;}&lt;BR /&gt;print(payload)&lt;BR /&gt;data = base64.encodebytes('payload'.encode())&lt;BR /&gt;print(data)&lt;BR /&gt;url = '&lt;A href="https://192.168.15.4/web_api/v1.6.1/add-host" target="_blank"&gt;https://192.168.15.4/web_api/v1.6.1/add-host&lt;/A&gt;'&lt;BR /&gt;response = requests.request("POST", url, headers=headers, data=data, verify=False)&lt;BR /&gt;print(response.text)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is my output with the error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;$ python add_multi_host.py&lt;BR /&gt;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: &lt;A href="https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings" target="_blank"&gt;https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings&lt;/A&gt;&lt;BR /&gt;warnings.warn(&lt;BR /&gt;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: &lt;A href="https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings" target="_blank"&gt;https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings&lt;/A&gt;&lt;BR /&gt;warnings.warn(&lt;BR /&gt;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: &lt;A href="https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings" target="_blank"&gt;https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings&lt;/A&gt;&lt;BR /&gt;warnings.warn(&lt;BR /&gt;atVmfEs8DGqoRaEREqHdiV9uVbI6O0KsJnisBDnDtOc&lt;BR /&gt;{'[{"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"}]'}&lt;BR /&gt;b'cGF5bG9hZA==\n'&lt;BR /&gt;{&lt;BR /&gt;"code" : "generic_err_invalid_syntax",&lt;BR /&gt;"message" : "Payload is not a valid JSON"&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 17:24:00 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132561#M6366</guid>
      <dc:creator>Peter_Odedina</dc:creator>
      <dc:date>2021-10-25T17:24:00Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Python Script and Post Request</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132565#M6368</link>
      <description>&lt;P&gt;First of all, you realize you don't even need to write a Python script for this, right?&lt;BR /&gt;You can import using mgmt_cli and a specially crafted CSV.&amp;nbsp;&lt;BR /&gt;There are several examples in the community, but here's one recent discussion:&amp;nbsp;&lt;A href="https://community.checkpoint.com/t5/API-CLI-Discussion/MGMT-CLI-Object-import/m-p/108135#M5494" target="_blank"&gt;https://community.checkpoint.com/t5/API-CLI-Discussion/MGMT-CLI-Object-import/m-p/108135#M5494&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To your script: it's not clear to me if you're actually looping through the items and submitting each one individually to add-host or if you're trying to submit them all at once with a single call.&lt;BR /&gt;The output suggests you're trying to do the latter, which definitely work work.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 19:14:34 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132565#M6368</guid>
      <dc:creator>PhoneBoy</dc:creator>
      <dc:date>2021-10-25T19:14:34Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Python Script and Post Request</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132571#M6370</link>
      <description>&lt;P&gt;Thanks for your response for some reason i have not been able to get the mgm_cli api call work with csv file call. It always return one error or the other that is what push me to try it in python but I hit another brick wall.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Back to the script,&amp;nbsp; yes I was trying to submit all the items in the csv file all at once to create the objects. I was able to use the run iterative tool in Postman to achieve the same result but I need to make several API calls to login, publish, add object, publish again etc to make it work. So i was hoping to just put all these api calls together in one python script.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 20:53:24 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132571#M6370</guid>
      <dc:creator>Peter_Odedina</dc:creator>
      <dc:date>2021-10-25T20:53:24Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Python Script and Post Request</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132572#M6371</link>
      <description>&lt;P&gt;The order of operations should be something like:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Login&lt;/LI&gt;
&lt;LI&gt;"Do Stuff"&lt;/LI&gt;
&lt;LI&gt;Publish&lt;/LI&gt;
&lt;LI&gt;Logout&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;In the "do stuff" step, you'd loop through and do multiple add-host commands.&lt;BR /&gt;And yes, that's how the API works.&lt;BR /&gt;That said, we added support for a batch mode in R80.40.&lt;BR /&gt;See:&amp;nbsp;&lt;A href="https://sc1.checkpoint.com/documents/latest/APIs/index.html#cli/add-objects-batch~v1.6%20" target="_blank"&gt;https://sc1.checkpoint.com/documents/latest/APIs/index.html#cli/add-objects-batch~v1.6%20&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As for the CSV file method, the issue everyone has is the first line of the CSV file.&lt;BR /&gt;Given the example you provided, the first line would be: name,ip-address,color&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 21:34:11 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132572#M6371</guid>
      <dc:creator>PhoneBoy</dc:creator>
      <dc:date>2021-10-25T21:34:11Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with Python Script and Post Request</title>
      <link>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132652#M6374</link>
      <description>&lt;P&gt;Thanks for the mgmt_cli works for me for the first time. I just need to specify the correct path to the file and run it from the right directory. I guess you are right i don't need the python script after all. I share the command below for the benefit of all:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;C:\Program Files (x86)\CheckPoint\SmartConsole\R80.40\PROGRAM&amp;gt;mgmt_cli -m 192.168.15.4 -u username -p ***** add host -b C:\Users\OdedinaP\Python_Learning\Add-Multi-Host\add_mutli_host.csv&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;---------------------------------------------&lt;BR /&gt;Time: [10:26:43] 26/10/2021&lt;BR /&gt;---------------------------------------------&lt;BR /&gt;"Publish operation" in progress (80%)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;---------------------------------------------&lt;BR /&gt;Time: [10:26:53] 26/10/2021&lt;BR /&gt;---------------------------------------------&lt;BR /&gt;"Publish operation" succeeded (100%)&lt;/P&gt;&lt;P&gt;C:\Program Files (x86)\CheckPoint\SmartConsole\R80.40\PROGRAM&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 16:32:17 GMT</pubDate>
      <guid>https://community.checkpoint.com/t5/API-CLI-Discussion/Problem-with-Python-Script-and-Post-Request/m-p/132652#M6374</guid>
      <dc:creator>Peter_Odedina</dc:creator>
      <dc:date>2021-10-26T16:32:17Z</dc:date>
    </item>
  </channel>
</rss>

