basicform-2

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.