Post Method Error

ruuhverr
Kilo Explorer

Hello Everone,

I want to create a record using Scripted Rest Api.

Here is my code:

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

var gr=new GlideRecord('sn_customerservice_case');

gr.initialize();

gr.description="Testing going on"

gr.impact="4";

var abc=gr.insert();

return "Sys id is "+abc;

})(request, response);

When I am inserting a its showing this error from POSTMAN

Error:

{

      "error": {

              "detail": "",

              "message": "java.lang.IllegalArgumentException: Cannod decode: java.io.StringReader@1142887"

      },

      "status": "failure"

}

1 ACCEPTED SOLUTION

Hi Rahul,



In postman add the content body in the body part and select raw as the format then you should get an area to put the json request body and add the body there and hit the endpoint


format in your earlier screenshot is form-data make it to raw



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Rahul,



So you are using post method. Are you sending the request body as well in the postman when calling this api endpoint.


Can you share screenshot of the postman tool.



Also you are not sending the response in correct format.



Correct format is:



var responseBody = {};


responseBody.sysId= abc;


responseBody.status = "Success";


response.setBody(responseBody);



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

HI Ankur , Thanks for the reply


postman.png


I am sending the 2 key:value in request body. When I hit the api it shows this error. For now my main focus is on inserting the data than on the resonse.


Hi Rahul,



You need to send the body as json key value pair and fetch the body content in the scripted rest api and then parse the request body, insert record and return response.


following post which I commented on should help you:


https://community.servicenow.com/thread/276281



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,



postman1.png


This is the code that I have written on the Scripted Rest. I checked in the log and I am able to retreive desc value. But I am not able to understand why this error is coming for the Postman as if i try from Rest Api Explorer I am able to insert the data



postman1.png



And this is Response that I am getting


postman1.png



I tried to hit this Api with the Soap UI and I am able to insert the data.


postman1.png



But I am not able to insert to data from Postman and I have to show   demo to my client from Postman only.