spring-cloud / spring-cloud/spring-cloud-gateway

How to Validate a Specific Value in the Request Body of a Multipart POST Request in Spring Cloud Gateway MVC?

Open
#3,765 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

Hi,

In our Spring Cloud Gateway MVC project, we need to validate a specific value in the request body of a multipart POST request before routing it to a downstream microservice.

Currently, we're reading the request in a before() filter, but this causes the body to appear empty when the RestClient forwards the request to the backend service. It seems this happens because the multipart stream is consumed and not cached.

From our understanding, a solution would require a version of GatewayMultipartHttpServletRequest that supports caching of the full multipart request.

Questions:

  • Will support for multipart request caching be added in a future release?
  • Is there a recommended workaround in the meantime?
  • Is there a more elegant or idiomatic way to perform this kind of validation in Spring Cloud Gateway MVC?

Thanks in advance for your help!

Router configuration example

    return route()
        .POST(
            “path”
            https())
        .before(validate())
        .build();

Before filter example

public static Function<ServerRequest, ServerRequest> validate() {

  return request -> {

    var servletRequest = request.servletRequest();

    if (servletRequest instanceof MultipartHttpServletRequest multipartRequest) {
      try {
        var data = multipartRequest.getPart("data");
        if (data != null) {
           // Validation logic based on data part of multipart request
        }
      } catch (IOException | ServletException e) {
        throw new RuntimeException(e);
      }
    }
  };
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the MVC before() filter path, GatewayMultipartHttpServletRequest, and the RestClient forwarding behavior shown in the issue. Determine whether multipart request caching is supported or needs a workaround, and document or implement the recommended idiomatic validation approach without leaving the forwarded request body empty.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.