Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
bhaizlett123
Participant

cp_mgmt_api_python_sdk passing "--format json"

I started to use the cp_mgmt_api_python_sdk in python and works great. One question though is there a way to pass the "--format json" parameter to it when using it to make an api call like show-group name "testgrp" --format json

Example - 

client.api_call("show-group", {"name": "testgrp"}, "--format json")

 

 

4 Replies
PhoneBoy
Admin
Admin

The API responds in JSON when you call it directly.
mgmt_cli converts the output into some sort of text output unless you call it with —format json.
Not sure what this Python module is doing.
Highly recommend looking at other examples in the community, for example: https://community.checkpoint.com/t5/API-CLI-Discussion-and-Samples/Python-tool-for-exporting-importi...

Art_Zalenekas
Employee
Employee

If you follow backward the code in the SDK, you will see that the api_call function returns an object back with the type of APIResponse. If you look at the class APIResponse, you will see that the variable data is a dict. So load that variable with json.dumps()  function which will return JSON in string, then use json.loads() to return JSON data. Also, the default return/response (look at special variable __repr__) is a two-string format. You could definitely grab that output in parentheses as that is already json.dumps() and preformatted.

def __repr__(self)
  return '%s(%s)' % (type(self).__name__, json.dumps(self.as_dict(), indent=4, sort_keys=True))

Hopefully, that helps!

0 Kudos
PhoneBoy
Admin
Admin

Looks like your code got emojied.
You might want to try again and enclose it in a code block 🙂

0 Kudos
Art_Zalenekas
Employee
Employee

PhoneBoy, sorry, that was copy/paste from our Python API SDK. I put it in the code snippet now.
Thanks!

0 Kudos