REST Script API giving bad json format error

booher04
Tera Guru

REST API Call to 3rd party(AlgoSec) is giving BAD JSON Format response in ECC Queue:

The format I was told to use is one I'm unfamiliar with, however when trying to use their format I get the same JSON error.  In the content section of the HTTP Method:

{
"template": "Standard",
"requestor":"${requestor}",
"change_request_justification":"${change_request_justification}",
"external_change_request":"${external_change_request}",
"traffic": [{ "source": {"items": [{ "name": ""}] },
"destination": {"items": [{ "name": "}] },
"service": {"items": [{"name": "" }] },
"action": "Allow",
"application": {"items": [{"name": ""}]} }]
}

"Traffic" seems to be the equivalent to a MRVS we have in SNOW to pass over to AlgoSec.

Here is the Run Script POST in my workflow:

try { 
 var r = new sn_ws.RESTMessageV2('AlgoSec', 'AlgoSec POST');
 r.setRequestHeader('Cookie',current.variables.sessionid.toString());
 
 r.setStringParameterNoEscape('template', 'Standard');
 r.setStringParameterNoEscape('requestor', current.variables.requester_name.getDisplayValue());
 r.setStringParameterNoEscape('change request justification',current.variables.chg_justification);
 r.setStringParameterNoEscape('external change request id', current.number);
 r.setStringParameterNoEscape('traffic', current.variables.access_request_detail);
 r.setStringParameterNoEscape('action', 'Allow');

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}

current.variables.xml_response = responseBody;

 I authenticate correctly, then the POST happens and I get the below back in the ECC queue input:

<results error="Method failed: (/FireFlow/api/change-requests/traffic) with code: 400" probe_time="209" result_code="900000">
<result error="Method failed: (/FireFlow/api/change-requests/traffic) with code: 400">
<output>{"status":"Failure","messages":[{"code":"BAD_JSON_FORMAT","message":"Bad JSON format: failed to parse request body"}],"data":null}</output>
</result>

Response content:

"content" value="{ "template": "Standard", "requestor":"Jeffrey Booher", "change_request_justification":"", "external_change_request":"", "traffic": [{ "source": {"items": [{ "name": ""}] }, "destination": {"items": [{ "name": "}] }, "service": {"items": [{"name": "" }] }, "action": "Allow", "application": {"items": [{"name": ""}]} }] }"/>

 I'm assuming the format I'm using in the Content of the HTTP Method and the Run Script POST in the workflow are not formatted correctly, so any pointers would be great. 

 

Typically when passing MRVs I use "name_of_mrvs":${name_of_mrvs} and the variables I use "name_of_var":"${name_of_var}",  

1 ACCEPTED SOLUTION

Hello @booher04 

 

It's because you are calling the rest message function outside the loop. It's having only first initialized value, that's why working for only one. 

 

Please use below 👇 script - 

 

var mrvs = current.variables.access_request_detail;
var rowCount = mrvs.getRowCount();

// Function to send REST request
function sendRestRequest(dest, sourceIp, destIp) {
try {
var r = new sn_ws.RESTMessageV2('AlgoSec', 'AlgoSec POST');
r.setRequestHeader('Cookie', current.variables.sessionid.toString());

r.setStringParameterNoEscape('service', dest);
r.setStringParameterNoEscape('application', "any");
r.setStringParameterNoEscape('source', sourceIp);
r.setStringParameterNoEscape('destination', destIp);

var response = r.execute();
gs.info('Request body: ' + r.getRequestBody());

return response.getBody();
} catch (ex) {
gs.error('REST request failed: ' + ex.message);
return null;
}
}

// Loop through the MRVS contents
var responseBody = '';
for (var i = 0; i < rowCount; i++) {
var row = mrvs.getRow(i);
var dest = row.destination_tcp_port;
var sourceIp = row.source_ip_address_es;
var destIp = row.destination_ip_address_es;

responseBody = sendRestRequest(dest, sourceIp, destIp);
}

// Store the last response in the variable
current.variables.xml_response = responseBody;

 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

View solution in original post

23 REPLIES 23

Hello @booher04 

 

One more thing rhe xml.response variable in this case will only store the last response, so you might use list - if you want to store all the responses. 

 

If you want this update you can let me know. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Ankur Bawiskar
Tera Patron
Tera Patron

@booher04 

are you sure you are sending the MRVS data in that JSON correctly as per what they expect?

what came in the request body?

try {
 var r = new sn_ws.RESTMessageV2('AlgoSec', 'AlgoSec POST');
 r.setRequestHeader('Cookie',current.variables.sessionid.toString());
 
 r.setStringParameterNoEscape('template', 'Standard');
 r.setStringParameterNoEscape('requestor', current.variables.requester_name.getDisplayValue());
 r.setStringParameterNoEscape('change request justification',current.variables.chg_justification);
 r.setStringParameterNoEscape('external change request id', current.number);
 r.setStringParameterNoEscape('traffic', current.variables.access_request_detail);
 r.setStringParameterNoEscape('action', 'Allow');

 var response = r.execute();
 gs.info('Request body' + r.getRequestBody());
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.message;
}

current.variables.xml_response = responseBody;


If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Request body{
"template": "Standard",
"requestor":"Jeffrey Booher",
"change_request_justification":"",
"external_change_request":"",
"traffic": [{ "source": {"items": [{ "name": ""}] },
"destination": {"items": [{ "name": ""}] },
"service": {"items": [{"name": "" }] },
"action": "Allow",
"application": {"items": [{"name": ""}]} }]
}

Request body{
"template": "Standard",

"traffic": [
{
"source": {
"items": [
{
"address": "[10.0.0.2]"
}
]
},
"destination": {
"items": [
{
"address": "[10.0.0.5]"
}
]
},
"service": {
"items": [
{
"address": "[22]"
}
]
},
"application": {
"items": [
{
"name": "1Password - CINC [Integration]"
}
]
},


"action": "Allow"
}
]
}