Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
viet_le_minh
Explorer
Jump to solution

Web API run-script output no responseMessage result

Hi,

I've been working with R80.30 API with Python REST calls.

I'm trying to run script show SIC Certificate with command: cpca_client lscert -kind SIC

When I'm testing in Postman, everything OK. Here are the steps to test with Postman:

1) login

2) run-script:

{
  "script-name" : "Script Example",
  "script" : "cpca_client lscert -kind SIC",
  "targets" : [ "Test-SMC" ]
}
3) show-task:
{
  "task-id" : "b423063e-3d48-462b-9bc1-53f58b40b210",
  "details-level" : "full"
}
Then I write the script in Python. Here is the script:
import requests
import json
import base64

# 1. Login
 
# 2. Run Script
payload_run_script="{\r\n  \"script-name\" : \"Script show SIC Certificate\",\r\n  \"script\" : \"cpca_client lscert -kind SIC\",\r\n  \"targets\" : [ \"Test-SMC\" ]\r\n}"
headers = {
  'Content-Type': 'application/json',
  'X-chkp-sid': '{}'.format(sid_value),
  'Cookie': 'Session=Login'
}
response_run_script = requests.request("POST", url_run_script, headers=headers, data=payload_run_script, verify=False)
print(response_run_script.text)
task_id_value = (json.loads(response_run_script.text)['tasks'][0]['task-id'])

# 3. Show Task
payload_show_task="{\r\n  \"task-id\" : \""+task_id_value+"\",\r\n  \"details-level\" : \"full\"\r\n}"
headers = {
  'Content-Type': 'application/json',
  'X-chkp-sid': '{}'.format(sid_value),
  'Cookie': 'Session=Login'
}
response_show_task = requests.request("POST", url_show_task, headers=headers, data=payload_show_task, verify=False)

#Decode base64 from resonpeMessage
response_message = (json.loads(response_show_task.text)['tasks'][0]['task-details'][0]['responseMessage'])
response_mess_file = open("output.json", "w")
response_mess_file.write(response_show_task.text)
response_mess_file.close()
sic_cert_output = base64.b64decode(response_message)
print(sic_cert_output)

#Export SIC Cert output to file
sic_cert_file = open("SIC_cert.txt", "w")
sic_cert_file.write(str(sic_cert_output).replace('\\n', '\n'))
sic_cert_file.close()
 
When I run this script, the responseMessage field does not return a result. I had to try running the script 10 times to get the result one.
 
Payload: {
"tasks" : [ {
"uid" : "aa9c4e4a-4cd2-4033-a7e6-166b878827e3",
"name" : "Test-SMC - Script show SIC Certificate",
"type" : "CdmTaskNotification",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"task-id" : "88c1a492-155c-4243-9711-224f28f367ed",
"task-name" : "Test-SMC - Script show SIC Certificate",
"status" : "in progress",
"progress-percentage" : 10,
"start-time" : {
"posix" : 1607337340124,
"iso-8601" : "2020-12-07T17:35+0700"
},
"last-update-time" : {
"posix" : 1607337340124,
"iso-8601" : "2020-12-07T17:35+0700"
},
"suppressed" : false,
"task-details" : [ {
"uid" : "4aff5b60-ce30-4ceb-af0a-6584df9fe949",
"name" : null,
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"color" : "black",
"statusCode" : "in progress",
"statusDescription" : "",
"taskNotification" : "aa9c4e4a-4cd2-4033-a7e6-166b878827e3",
"gatewayId" : "051adfc9-77e4-7143-aacc-e43cbbb2e38c",
"gatewayName" : "",
"transactionId" : 273232546,
"responseMessage" : "",
"responseError" : "",
"meta-info" : {
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1607337340155,
"iso-8601" : "2020-12-07T17:35+0700"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1607337340155,
"iso-8601" : "2020-12-07T17:35+0700"
},
"creator" : "admin"
},
"tags" : [ ],
"icon" : "General/globalsNa",
"comments" : "",
"display-name" : "",
"customFields" : null
} ],
"comments" : "In Progress...",
"color" : "black",
"icon" : "General/globalsNa",
"tags" : [ ],
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1607337340148,
"iso-8601" : "2020-12-07T17:35+0700"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1607337340148,
"iso-8601" : "2020-12-07T17:35+0700"
},
"creator" : "admin"
},
"read-only" : false
} ]
}
 
Please help me. Thanks.
 
Regards,
0 Kudos
1 Solution

Accepted Solutions
Danny
Champion Champion
Champion

You posted the solution yourself. Look at your output, it writes "progress-percentage" : 10statusCode" : "in progress"

Your script needs to loop asking for the task-id until the response is 'succeeded'.

View solution in original post

0 Kudos
2 Replies
Danny
Champion Champion
Champion

You posted the solution yourself. Look at your output, it writes "progress-percentage" : 10statusCode" : "in progress"

Your script needs to loop asking for the task-id until the response is 'succeeded'.

0 Kudos
viet_le_minh
Explorer

Dear Danny,

Great. I have tried to set time sleep 3 seconds, and the results came out.

Thank you so much.

0 Kudos

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events