spring-cloud/spring-cloud-gateway

Modify Request Body for a GET request

Open

#1147 opened on Jul 3, 2019

View on GitHub
 (7 comments) (0 reactions) (0 assignees)Java (4,284 stars) (3,204 forks)batch import
enhancementhelp wanted

Description

We currently have custom code for modifying request body which works but I want to move to the standard ModifyRequestBodyGatewayFilterFactory.

We have a scenario where Spring Cloud Gateway received a GET request and we need call an internal GraphQL api with a POST and a request body.

ModifyRequestBodyGatewayFilterFactory works great if there is a body present in the original request but doesn't apply the rewriteFunction when there is no body on GET requests.

I am using v2.1.2.RELEASE and I have hacked together a change to ModifyRequestBodyGatewayFilterFactory which works for me:

Mono<?> modifiedBody = serverRequest.bodyToMono(inClass)
        .defaultIfEmpty("") //added this line
        .flatMap(o -> config.rewriteFunction.apply(exchange, o));

I appreciate that the ModifyRequestBodyGatewayFilterFactory is beta, but would it be possible to support adding a body for GET requests in the future?

Contributor guide