Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Custom Response for Scripted REST API Error

Babuloo47
Tera Expert

Hi,

 

I want to send an Custom response for 404 Error in Scripted REST API

 

As of now we can acheive the below response for Custom error responses.

 

"error": {

message: "",

detail: ""

},

"status": "failure"

 

I would like to know is it possible to acheive like below.

 

"error": {

response_code : "",

response_details: "",

status: ""

}

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron

Hi @Babuloo47 

yes it is possible. Follow the following pattern

var _objResponseError = {
  "error": {
    response_code : "",
    response_details: "",
    status: ""
  }
}
var _strResponseError = JSON.stringify(_objResponseError, null, 4);

response.setStatus(404);
response.setContentType('application/json');
response.getStreamWriter().writeString(_strResponseError);

Maik

View solution in original post

2 REPLIES 2

Maik Skoddow
Tera Patron

Hi @Babuloo47 

yes it is possible. Follow the following pattern

var _objResponseError = {
  "error": {
    response_code : "",
    response_details: "",
    status: ""
  }
}
var _strResponseError = JSON.stringify(_objResponseError, null, 4);

response.setStatus(404);
response.setContentType('application/json');
response.getStreamWriter().writeString(_strResponseError);

Maik

Thanks for the help! It Indeed works.