In ServiceNow processors, you have the nice getParameter() function to get a POST or GET url parameter. However, processors work differently depending on the Content-Type of the HTTP request. I wrote a few functions that work with three of the more popular content types that I use.
application/x-www-form-urlencoded
With this Content-Type the information comes in as one URL parameter. That URL parameter name is a JSON string of all of the posted information. There is no value to that parameter. You have to get the enumeration of the parameters and then convert the parameter to a Javascript object.
Here is the function:
1 2 3 4 5 6 7 | function getURLEncodedFormParam(paramName) { var urlParamList = g_request.getParameterNames(); var paramsEl = urlParamList.nextElement(); var json = new JSON(); var params = json.decode(paramsEl); return params[paramName]; } |
multipart/form-data
This Content-Type will store the various parameters as a JSON string in the body of the request. You need to read the body of the request and then convert it to a Javascript Object.
1 2 3 4 5 6 7 | function getMultipartFormDataParameter(paramName){ var is = g_request.getInputStream(); var sb = GlideStringUtil.getStringFromStream(is); var json = new JSON(); var params = json.decode(sb); return params[paramName]; } |
The “application/json” content-type will also use this method of grabbing parameters.
Hi John,
I have a requirement of fetching the contents of file pushed through a form and creating a custom attachment in a different table. I utilized the above multipart/form-data script to fetch the contents of POST. I was able to utilize many functions(cookies, headers, content type, content length) of interface HttpServletRequest which resembles the g_request object in processor but I am unable to use the methods getPart(name) and getParts() which is used to read the file content attached in the form.
g_request.getPart(name) — null
g_request.getParts() — []
The above are the outputs of using these methods. To fetch the file content and other related parameters, I have to use string functions : substr and substring.
I also tried using the JSON snippet in the blog to get values of form components but it gives undefined.
Do you have an idea of using the methods related to getPart/getParts() for reading the multipart/form-data components as shown in Java tutorial example : http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html
Please let me know your thoughts.
Regards,
Kshitij