I am writing a controller capable of handeling different types of post requests.
As a result I would like to be able to get the number of key value pairs in the request, then retrive them as some form of object (a list or some kind of iterator) so that I can loop through them retrieve their key and values and handle them appropriately.
Something like as follows:
def sz = request.size // may or may not be necessary, but would still like to know how
def keyValPairs = request.listOfPosts
for (def keyVal : keyValPairs) {
def key = keyVal.getKey();
def val = keyVal.getVal();
doSomething(key, val);
}
Thank you
Several things to mention here. First of all, it all depends on the encoding of the POST data. For example, if you send the data in JSON format, converting it to a map is different than if you send it as
Content-Type: application/x-www-form-urlencoded(that is the case if you send the data from a HTML form).If the latter is true, the only thing you need to do is to use
getParameterMap()from plain oldServletRequest. Grails only overrides this method to provide a data structure that plays nicely with routing, namely GrailsParameterMap.