Unable to read request body from ServletRequest twice because InputStream is getting exhausted after first use
mutyasaisrikar opened this issue · 2 comments
I am using Java 17, Spring Boot 3 and logbook-spring-boot-starter:3.9.0
I have a Spring interceptor where I have to read the request body in my application. I also use Logbook to log the request and the response in custom format.
I was previously using the 1.13.0
version of Logbook. After migrating to the latest version, I can read the request body in my Spring interceptor but it is failing at @RequestBody
annotation in the Spring Controller class with an error saying Required request body is missing
Possible Fix
After going through the code, I can see that previously with logbook 1.x, the underlying logic of httpServletRequest.getInputStream
method was creating a ByteArrayInputStream
object every time it gets called. So If I were to call getInputStream 2 times, it returns 2 different ByteArrayInputStream
objects.
But now It is getting created once per request. Because of that, the first time getInputStream is called, the stream is closed and the second time we get an empty response body.
Steps to Reproduce
https://github.com/mutyasaisrikar/logbook-error
- SpringConfig.java -> TestInterceptor class - reads request body and prints
RestApiTest
test fails because the Controller class is not able to read the request body
Hello @whiskeysierra,
This links to a similar issue I have raised before.
At that time I was not able to understand. But now I have more information about this. Can you please help with this?