Cannot access HTTP request body in Scriped Rest API

Volker1
Kilo Contributor

Hello everyone,

when I try to access the request body of an HTTP request generated by Postman, I get the following error:

"com.glide.rest.domain.ServiceException: Service error: attempt to access request body is not allowed 
when request method is GET or DELETE",

I don't understand this. I'm not using GET here, I use POST. The resource I created on my API on Snow also uses POST.

I also pass values in the request body, that's why I need to access it. Content type is application/x-www-form-urlencoded which is also supported by my resource in Snow.

The second line causes the error:

        // read values in HTTP body
        requestBody = request.body;
        requestBodyData = requestBody.data;

Can someone please help me?

1 ACCEPTED SOLUTION

Hi,

I believe for Content Type as application/x-www-form-urlencoded it transfers data in url as key value pair and not as body

check below links they might be helpful:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST

https://github.com/github/fetch/issues/263

https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data

simple example below

application/x-www-form-urlencoded - this tells the web server that you will be encoding the parameters in the URL as in

short_description=Testing&comments=testing+comments

application/json - this tells the web server that you are posting JSON data as in

{"short_description":"Testing","comments":"testing comments"}

possibly you need to use this to get the URL; print in logs and check

request.pathParams or request.queryString

Mark Correct if this solves your issue and also mark 👍 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

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you need to use below and then parse it

var reqbody = request.body.dataString;

var parser = new global.JSON();

var parsedData = parser.decode(reqbody);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

No this does not work.

The line

    var reqbody = request.body.dataString

is still causing the same error.

Hi,

How are you sending the json body as request? can you share in postman?

It should be as per below screenshot:

find_real_file.png

Content-Type should be application/json

find_real_file.png

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

I really appreciate your effort and your help, but please read my post more carefully.

I mentioned that I want to use application/x-www-form-urlencoded, not application/json.

PS: my solution works with application/json anyways. But I still would like to know why application/x-www-form-urlencoded is not working.

(And yes, I edited the supported formats in my API as well and added application/x-www-form-urlencoded)