How to set correct request using script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 02:13 AM
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:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2023 04:05 AM - edited 11-02-2023 01:44 AM
var status = workflow.scratchpad.appstatus;
var collibra_id = current.variables.colib_task_id.toString();
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');
var body = {"taskIds": [
collibra_id
],
"formProperties": {
"success": approvalStatus1,
"failure": approvalStatus2
}
};
r_colli.setRequestBody(JSON.stringify(body));
var responseFromColli = r_colli.execute();
var responseBody = responseFromColli.getBody();
var httpstatus = responseFromColli.getStatusCode();
workflow.info('Response body' + responseBody);
workflow.info('status' + httpstatus);
} catch (ex) {
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2023 12:35 AM
Hi Shruthi,
Getting the same error again after using above code. Please suggest.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2023 01:45 AM
Please try with the updated code
var status = workflow.scratchpad.appstatus;
var collibra_id = current.variables.colib_task_id.toString();
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');
var body = {"taskIds": [
collibra_id
],
"formProperties": {
"success": approvalStatus1,
"failure": approvalStatus2
}
};
r_colli.setRequestBody(JSON.stringify(body));
var responseFromColli = r_colli.execute();
var responseBody = responseFromColli.getBody();
var httpstatus = responseFromColli.getStatusCode();
workflow.info('Response body' + responseBody);
workflow.info('status' + httpstatus);
} catch (ex) {
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2023 12:58 AM
If one looks at the reference for method setRequestBody(), one will notice that a string is expected as parameter. So the system will apply method toString() to the object passed in as parameter to setRequestBody() and it will come out as [object Object] - which is not a valid JSON.
So instead of
r_colli.setRequestBody({
"taskIds": [
"${colli_id}"
],
"formProperties": {
"success": "${approvalStatus1}",
"failure": "${approvalStatus2}"
}
});
try
var requestBody = {
"taskIds": [
"${colli_id}"
],
"formProperties": {
"success": "${approvalStatus1}",
"failure": "${approvalStatus2}"
}
},
requestBodyJSON = JSON.stringify(requestBody);
r_colli.setRequestBody(requestBodyJSON);
But that is still not OK, cause tokens ${colli_id}, ${approvalStatus1} and ${approvalStatus2} are not replaced/"resolved".
Since you are providing the request body yourself, you should not be using parameters.
Simply write:
var requestBody = {
"taskIds": [
collibra_id // I'm assuming colli_id should get the value in collibra_id
],
"formProperties": {
"success": approvalStatus1,
"failure": approvalStatus2
}
},
...
instead.
Also for the sake of humanities survival and thriving, please always format your code. 🙂