Skip to content

Return 415 for form data via @RequestBody in WebFlux #26386

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

Closed
huberchrigu opened this issue Jan 14, 2021 · 1 comment
Closed

Return 415 for form data via @RequestBody in WebFlux #26386

huberchrigu opened this issue Jan 14, 2021 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@huberchrigu
Copy link

A @RestController like

@RestController
 public class DemoController {
     @PostMapping
     public Map<String, Object> getAttributes(@RequestBody Body body, ServerWebExchange serverWebExchange) {
         return serverWebExchange.getAttribute(body.getAttribute());
     }
 }

is expected to handle requests with content type application/json. When the client request has a content type x-www-form-urlencoded, I would expect that the server returns 415 Unsupported Media Type or another client error.
Instead Spring throws java.lang.IllegalStateException: In a WebFlux application, form data is accessed via ServerWebExchange.getFormData().

A sample application with failing test can be found here: https://github.com/huberchrigu/spring-reactive-wrong-content-type

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 14, 2021
@rstoyanchev rstoyanchev self-assigned this Jan 14, 2021
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Jan 14, 2021
@rstoyanchev
Copy link
Contributor

Technically it is possible to read the body via FormHttpMessageReader but we decided to prevent it as part of #22486 in order to avoid running into the issue of trying to consume the body twice, once via ServerWebExchange#getFormData (e.g. in a filter or other) and a second via @RequestBody. You're right though that 415 would be a better way to flag this since this effectively means that method doesn't support it.

@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 14, 2021
@rstoyanchev rstoyanchev added this to the 5.3.4 milestone Jan 14, 2021
@rstoyanchev rstoyanchev changed the title IllegalStateException for form content type and @RequestBody on reactive stack Return 415 for form data via @RequestBody in WebFlux Jan 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants