how can I modify the request message body at filters level. Can we change the message body with our custom message using RequestWrapper.
Asked
Active
Viewed 2,640 times
1
I Bajwa PHD
- 1,708
- 1
- 20
- 42
kunal kashyap
- 215
- 1
- 4
- 8
-
http://stackoverflow.com/a/1561963/174184 – TJ- May 06 '15 at 05:37
-
@TJ- - how does that relate to this? I mean, there you read the body, but there is no editing at all. – Joel Peltonen Jan 18 '17 at 11:34
1 Answers
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
-
-
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