Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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
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
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.