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

Oct_vio Augusto
Tera Contributor

Hello,

I´m facing the same problem.
The media type is application/json. Need I change another parameter?

find_real_file.png

I had a similar message, i had application/json and i had raw but my issue was that i had a rogue apostrophe which meant i didn't have valid JSON. If you've double checked you're JSON and it looks correct, maybe copy and paste it into a JSON checker.

 

Hope this helps!