kunal kashyap
kunal kashyap

Reputation: 265

Modify request message body

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

Upvotes: 1

Views: 2895

Answers (1)

Costi Ciudatu
Costi Ciudatu

Reputation: 38255

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.

Upvotes: 3

Related Questions