This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Cloud Gateway 4.2.0!

ModifyRequestBody Filter

You can use the ModifyRequestBody filter to modify the request body before it is sent downstream by the gateway.

This filter can be configured only by using the Java DSL.

The following listing shows how to modify a request body filter:

GatewaySampleApplication.java
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.modifyRequestBody;
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
import static org.springframework.cloud.gateway.server.mvc.predicate.GatewayRequestPredicates.host;
import org.springframework.http.MediaType;

@Configuration
class RouteConfiguration {

    @Bean
    public RouterFunction<ServerResponse> gatewayRouterFunctionsAddReqHeader() {
		return route("rewrite_request_obj")
				.route(host("*.rewriterequestobj.org"), http("https://example.org"))
					.before(modifyRequestBody(String.class, Hello.class, MediaType.APPLICATION_JSON_VALUE,
								(request, s) -> new Hello(s.toUpperCase())))
					.build();
    }

	record Hello(String message) { }
}
If the request has no body, the RewriteFilter is passed null. Mono.empty() should be returned to assign a missing body in the request.