The CreatorCon Call for Content is officially open! Get started here.

How to set correct request using script

Akhil2001
Tera Contributor

Hi Everyone,

I'm getting below error while calling api.

 

Error: Response body{"statusCode":400,"titleMessage":"Bad Request","helpMessage":"Please correct the input object","userMessage":"There was an error while processing JSON content"}

 

Code:

var status = workflow.scratchpad.appstatus;
var collibra_id = current.variables.colib_task_id;
workflow.info(collibra_id + ',' + status);
var approvalStatus1;
var approvalStatus2;
 
if (status == "true") {
    approvalStatus1 = "true";
    approvalStatus2 = "false";
 
} else {
    approvalStatus1 = "false";
    approvalStatus2 = "true";
 
}
var number = current.number;
try {
    var r_colli = new sn_ws.RESTMessageV2('Collibra', 'sentResponseToColli');
 
 
  
   
 
    r_colli.setRequestBody({"taskIds": [
            "${colli_id}"
        ],
        "formProperties": {
            "success": "${approvalStatus1}",
            "failure": "${approvalStatus2}"
    }
});
    var responseFromColli = r_colli.execute();
    var responseBody = responseFromColli.getBody();
    var httpstatus = responseFromColli.getStatusCode();
    workflow.info('Response body' + responseBody);
    workflow.info('status' + httpstatus);
 
 
 
 
} catch (ex) {
 
}
8 REPLIES 8

Akhil2001
Tera Contributor

Hi,

I have followed the same as above, still getting the same error. Please help.

It would help if you posted exactly what you tried.

Akhil2001
Tera Contributor

Hi,

I'm sending below request to 3rd party application.

 

var requestBody = {
"taskIds": [
collibra_id
],
"formProperties": {
"success": approvalStatus1,
"failure": approvalStatus2
}
};

 

Everything is good, but  issue here is "taskIds": [
collibra_id
],  getting 400 bad request because of this when i given value of collibra_id directly in double quotation marks it is working fine im getting correct response when i given dynamic value like collibra_id change depends up on value from catalog form. When i printed request body in the logs it is showing as empty.

{"taskIds":[{}],"formProperties":{"success":"false","failure":"true"}}

What you write makes me to believe that you define variable collibra_id as you originally did (

 

var collibra_id = current.variables.colib_task_id;

 

) not following @Shruti 's advice (

 

var collibra_id = current.variables.colib_task_id.toString();

 

).

 

In ServiceNow fields (on GlideRecords, e.g. current.number) and variables (e.g. current.variables.colib_task_id) are NOT primitive values (like "A", 2, false), but objects.

Thus when coerced to string - like when they are (JSON) stringified, those end up as "{}" INSTEAD OF "A", "2" or "false" - even if in the database you will find those values ("A", 2 or false).

That is why when you need to get the actual value in a field or variable (JSON) stringif objects that contain values from fields or variables you should force their first convert those into string - exactly what and how @Shruti suggested.