Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to read request body from ServletRequest twice because InputStream is getting exhausted after first use #1865

Open
mutyasaisrikar opened this issue Jun 26, 2024 · 2 comments
Labels

Comments

@mutyasaisrikar
Copy link

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

  1. SpringConfig.java -> TestInterceptor class - reads request body and prints
  2. RestApiTest test fails because the Controller class is not able to read the request body
@mutyasaisrikar
Copy link
Author

Hello @whiskeysierra,

This links to a similar issue I have raised before.

#974

At that time I was not able to understand. But now I have more information about this. Can you please help with this?

@artpaym
Copy link

artpaym commented Oct 1, 2024

@mutyasaisrikar Afaik, it's a default behavior that reading from request stream drains it making controllers fail, and it requires manual handling of that unfortunately. It seems that if Logbook in previous versions handled it for us silently, it was only a fortunate coincidence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants