Error in displaying a Body parameter in Scripted REST API

rcruzsnow
Kilo Contributor

Hello,

So I'm testing out my new Scripted REST API Code which should pretty much return the body parameters. The parameter that I'm passing has a name of short_description and it's is Short Description Sample.

Postman Error.JPG

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

      // implement resource here

  var sampleBody = request.body;

  var sampleEntry = sampleBody.nextEntry();

  var desc = sampleEntry.short_description;

  return {

  "short-description" : desc

  };

})(request, response);

Unfortunately, after running the message, an error output came out

{

  "error": {

      "detail": "",

      "message": "com.glide.rest.domain.ServiceException: Exception while reading request"

  },

  "status": "failure"

}

I tried looking for a fix and checked the best practice in making my function work but it seems that I'm already following it based on this ServiceNow documentation for Scripted REST API:

RESTAPIRequestBody - nextEntry()

Is there any other fix that you guys can recommend that I use to return the parameter? If so, what is it and thanks for your help in advance!

1 ACCEPTED SOLUTION

srinivasthelu
Tera Guru

Hi Raymund,



The allowed content- types are application/json, application/xmls for ServiceNow REST services. multipart/form-data is not allowed.



Just switch your postman settings from form-data to raw and paste json equivalent of your request, then it should work.



{


"short_description" : "test",


"test" : "test2"


}


View solution in original post

6 REPLIES 6

srinivasthelu
Tera Guru

Hi Raymund,



The allowed content- types are application/json, application/xmls for ServiceNow REST services. multipart/form-data is not allowed.



Just switch your postman settings from form-data to raw and paste json equivalent of your request, then it should work.



{


"short_description" : "test",


"test" : "test2"


}


Hello,


I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.


Could you please guide me how to implement this? Sample code would be great.


Input: Sys ID of the record
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"Sys ID": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"Sys ID": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}

rcruzsnow
Kilo Contributor

Thanks bro! Your suggestion fixed my problem, thanks a lot!


Glad it worked.




Do you want to mark this question as answered? How To Mark Answers Correct From Community Inbox