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

multipart body request is empty when using BeforeFilterFunctions rewritePath

Open
#3,663 0 comments 0 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

Describe the bug
I'm using spring gateway mvc as a simple gateway to route input request to backend app. My route config rewrite the host and the url base path.
It works well except for multipart requests. With a multipart request, the output body is empty.
After some research in my side to identify the problem, it seems the rewritePath function is the root cause. If I delete it of my route, multipart requests works.
I'm using the function rewritePath in org.springframework.cloud.gatexay.server.mvc.BeforeFilterFunctions in a RouterFunction policy.

Sample

To prove the problem, I reproduced the case with this simple code.

@Configuration
public class RouterConfiguration {
    private static final String WILDCARD = "/**";
    private static final String SEGMENT_TO_INCLUDE_REGEX = "(?<segment>.*)";
    private static final String SEGMENT_TO_ADD_REGEX = "${segment}";
    
    @Bean
    public RouterFunctions.Builder routerFunctionsBuilder() {
        return RouterFunctions.route();
    }

    @Bean
    public RouterFunction<ServerResponse> buildRoutes(RouterFunctions.Builder routerFunctionsBuilder) {
        var destinationHost = URI.create("http://localhost:9008/");
        
        routerFunctionsBuilder
                .POST("multipart" + WILDCARD, HandlerFunctions.http(destinationHost)) // multipart works with this line only
                // multipart don't work anymore in adding the filter (body is empty)
                .before(BeforeFilterFunctions.rewritePath("multipart" + SEGMENT_TO_INCLUDE_REGEX, "/api/multipart" + SEGMENT_TO_ADD_REGEX));

        return routerFunctionsBuilder.build();
    }

}

Versions
Spring Boot : 3.3.0
spring cloud starter gateway mvc : 4.1.5

related issues but not solved : 3449

** Expectations **
rewritePath function works with multipart requests

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 RouterFunction sample and inspect BeforeFilterFunctions.rewritePath alongside HandlerFunctions.http, reproducing the multipart request with and without the filter. Trace why the rewritten route forwards an empty body, then verify that multipart content reaches the backend while the path is rewritten to /api/multipart.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.