This is almost identical to the code above processing query parameters.  Is it possible to pull it into a method.