dakamojo
dakamojo

Reputation: 1955

HttpServletRequest getParameter AND getReader

I am developing a HttpServlet that handles a POST request from an application that includes parameters on the query string, and a JSON object in the body. It is not a form post, the entire body is the JSON object. I understand that I need to use HttpServletRequest.getReader() to read the body. But I also need to use HttpServletRequest.getParameter() to get the query parameters. And I understand that both can not be used.

I think the intended solution is to create a HttpServletRequestWrapper and override getReader() in such a way as to allow it to be called more than once. But I can't figure out how to do that. Or maybe this is not the intended approach. All the examples of HttpServletRequestWrapper that I can find seem to be related to creating filters and modifying the contents of the request.

Any help is greatly appreciated.

BTW, this is hosted on Google App Engine, but I don't think that will affect the solution.

Upvotes: 3

Views: 6271

Answers (2)

Dan
Dan

Reputation: 2177

I would suggest reading the query string parameters directly by parsing the query string. This way you won't touch the message body and you can use HttpServletRequest.getReader().

Parsing the query string is tricky but there it can be done safely using URLEncodedUtils from Apache Http Client package as discussed here:

Upvotes: 1

t_motooka
t_motooka

Reputation: 565

I think you can implement multiple-callable getReader() in your HttpServletRequestWrapper :

  1. save the HTTP-request body into a temporary file.
  2. when HttpServletRequestWrapper.getReader() is called, open the temporary file.
  3. delete the temporary file at the end of request processing.

to implement (1) and (3), ServletFilter may be useful.

Upvotes: 1

Related Questions