- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2020 07:00 AM
Hello everyone,
when I try to access the request body of an HTTP request generated by Postman, I get the following error:
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:
Can someone please help me?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 07:21 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2020 07:22 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2020 07:34 AM
No this does not work.
The line
var reqbody = request.body.dataString
is still causing the same error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2020 10:28 PM
Hi,
How are you sending the json body as request? can you share in postman?
It should be as per below screenshot:
Content-Type should be application/json
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2020 05:10 AM
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)