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

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

All right, this makes sense. x-www-form-urlencoded posts the parameters in the URL.

But still, it doens't make sense that I cannot access the request body.

Anyway, I solved it using application/json, because this worked. Thanks for your help! 🙂

Ahmed Farouk1
Tera Expert

Hello Mega,

Did you find a solution for this case we have the same issue.