1

how can I modify the request message body at filters level. Can we change the message body with our custom message using RequestWrapper.

I Bajwa PHD
  • 1,708
  • 1
  • 20
  • 42
kunal kashyap
  • 215
  • 1
  • 4
  • 8

1 Answers1

3

The short answer is yes.

However, you don't really modify the original request body; instead, you can return a different body from the request wrapper and the servlet will just work with that.

As for how you do it, just overwrite the getInputStream() method of the HttpServletRequestWrapper and return a modified version of the original InputStream.

To make sure you remove any trail of the original body, you may want to overwrite getReader() as well. Standard implementations would return some BufferedReader over your InputStream when asked for a reader, but there are mock implementations (like the one in spring-test) that don't.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • how can i return a different body for the request – kunal kashyap May 06 '15 at 05:42
  • I tried to answer with edits to my post above: you should overwrite `getInputStream()` (and `getReader()`, so you won't rely on the underlying `HttpServletRequest` implementation behavior). – Costi Ciudatu May 06 '15 at 06:08
  • I am using async filters to read post request and once i get all the data then i have to forward that data to underlying servlet for processing. How can I do It – kunal kashyap May 06 '15 at 06:46