Custom HTTP Status Code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 07:39 AM
Hi All,
I am working on an inbound integration(Webservice). If record is created, by default we get a Status Code '200', If its not created then we can see '400' status code. I want to throw Custom Status Code for partially updated record?
If yes, How can I achieve the same? Please suggest.
Thank you!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 07:54 AM
Hi,
Yes you can achieve it use below.
based on the status it will display the status code. sn_ws_err.ServiceError(); using this REST API object we can set the status code. You can set the status on your code based on requirement.
if (this.status == 200) {
this.response.setBody(respObj);
this.response.setStatus(this.status);
return this.response;
} else {
var sError = new sn_ws_err.ServiceError();
sError.setStatus(this.status);
sError.setMessage(respObj.body.message);
sError.setDetail(respObj.body.detail);
return sError;
}
respObj Example for success:
respObj.body = {
"message": "Update Successful!",
"detail": "Found Id and record updated."
};
Hope you got what I am trying to explain. let me know if you need anything.
Thanks,
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 08:22 AM
Hi Pavan,
Thanks for your response!!
Actually, I'm new to integration and it is little difficult to understand.
My requirement is, if optional fields in the Payload have incorrect values then those values should not be allowed to insert/update but, the response in this case should be custom status code. For ex. "206 - Partial Content".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2022 08:36 AM
Hi,
Nop. I will share you the simple example if that is not clear share your script here. I will be helping you.
In the below example if caller id is empty in JSON then it will return 400 or if it has some value then will return 200 and will create record in ServiceNow.
Scripted REST API:
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
this.request = request;
this.response = response;
var payload = request.body.data;
var callerid = payload.callerid;
var short_description = payload.short_description;
var description = payload.description;
this.status = '200';
var respObj = {};
if (callerid) {
var gr = new GlideRecord("incident");
gr.initialize();
gr.short_description = short_description;
gr.description = description;
gr.caller_id = callerid;
gr.insert();
this.status = '200';
respObj.body = {
"message": "create Successful!",
"detail": "Incident " + gr.number + "created successfully"
};
} else {
this.status = '400';
respObj.body = {
"message": "Record Ignored",
"detail": "Caller ID is empty in Payload"
};
}
if (this.status == '200') {
this.response.setBody(respObj);
this.response.setStatus(this.status);
return this.response;
} else {
var sError = new sn_ws_err.ServiceError();
sError.setStatus(this.status);
sError.setMessage(respObj.body.message);
sError.setDetail(respObj.body.detail);
return sError;
}
})(request, response);
JSON format:
{
"callerid": "",
"short_description": "Test short description",
"description": "Test description"
}
Error Response:
Thanks,
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2022 07:35 AM
Hi Ankita,
If your issue got resolved, please Mark ✅ Correct/Helpful.
Regards
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar