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

[ISSUE] Reading HttpServletRequest Multiple Times in Spring Additional explanation of the limited situation needed. #16486

Open
IMWoo94 opened this issue Apr 24, 2024 · 0 comments
Labels
on-jira triaged Issues reviewed by a dev and considered valid. Will be added in Jira.

Comments

@IMWoo94
Copy link

IMWoo94 commented Apr 24, 2024

Article and Module Links
Reading HttpServletRequest Multiple Times in Spring
CachedBodyHttpServletRequest

Describe the Issue
It creates a reusable request, but there is a problem if the data is provided in FORM format in the request body.
No parameters are defined for data in FORM format.

To Reproduce
Steps to reproduce the behavior:

  1. The first follow the guide for configuration. Reading HttpServletRequest Multiple Times in Spring
  2. Send an HTTP request. - contentType ( application/x-www-form-urlencoded )
  3. Check request parameters.

Expected Behavior
The "cachedBody" will contain the content of the HTTP request body.
However, information about the request parameters cannot be provided.

Because, in the process of creating a newly defined request, we call "Request getInputStream()" of the request.
This invocation alters the value of the "usingInputStream" field.
When calling the method to request information about the parameters, it proceeds to parse the parameters through the parseParameters() method.
The parseParameters() method determines whether to execute the parsing task based on the "usingInputStream" field.
As a result, a situation arises where the values for the parameters are not defined because they have already been read.

  1. Mentioning in the guide that normal operation may not occur in the case of FORM data format could be helpful.
  2. Adding code like " ContentCachingRequestWrapper " provided by Spring would be beneficial, as it allows handling of parameters for FORM data format separately.
  • The ContentCachingRequestWrapper class provided by Spring also invokes "super.getParameterMap()" to trigger the execution of the "parseParameters()" method.

The " fix CachedBodyHttpServletRequest " image below illustrates a simple approach where, upon receiving data in FORM format, the body is read to directly generate parameters.
Subsequently, when requesting parameters, both the existing request parameters and the generated FORM parameters are provided together.

I'm learning a lot and finding the tutorials provided very useful.
Updating the guide document could be beneficial and helpful to many people.

Screenshots

  • Custom Request Constructor
image
  • Request getInputStream() and usingInputStream
스크린샷 2024-04-24 오후 9 23 32
  • Request parseParameters()
image
  • ContentCachingRequestWrapper getParameters
image
  • fix CachedBodyHttpServletRequest
image image

Environment (please complete the following information):

  • OS: [Mac m2]
  • Develop Env
    • IntelliJ IDE
    • Spring boot 3.2.5
    • Spring Web
@apeterlic apeterlic added the triaged Issues reviewed by a dev and considered valid. Will be added in Jira. label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
on-jira triaged Issues reviewed by a dev and considered valid. Will be added in Jira.
Projects
None yet
Development

No branches or pull requests

3 participants