Get request.body from Spring Cloud Gateway filter

problem description

how to get complete request.body information in self-built Filter?

the environmental background of the problems and what methods you have tried

when learning Spring Cloud Gateway to write filter himself, I tried to get the request message body through exchange.getRequest () .getBody, but there was a problem that I could not get the complete message body content. I would like to ask all of you to help me to see what the problem is? How should it be adjusted? Thank you very much.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

@Component
public class PreGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {


    @Override
    public GatewayFilter apply(Object config) {
        // grab configuration from Config object
        return (exchange, chain) -> {
            //If you want to build a "pre" filter you need to manipulate the
            //request before calling change.filter

            //1Fluxbody
            ServerHttpRequest request = exchange.getRequest();
            Flux<DataBuffer> dataFlux = request.getBody();

            StringBuffer buffer = new StringBuffer();

            dataFlux.log().flatMap(dataBuffer -> {
                System.out.println("Thread - " + Thread.currentThread().getName() + ": readable count is  " +  dataBuffer.readableByteCount());
                byte[] bytes = new byte[dataBuffer.readableByteCount()];
                dataBuffer.read(bytes);
                DataBufferUtils.release(dataBuffer);
                return Mono.just(bytes);
            }).subscribe(s -> System.out.println(new String(s)));

             //2DefaultServerRequestBodyExtractorsMono<String>
            /*DefaultServerRequest request = new DefaultServerRequest(exchange);
            request.body(BodyExtractors.toMono(String.class)).log().subscribe(System.out::print);*/

            //use builder to manipulate the request
            ServerHttpResponse response = exchange.getResponse();
            return response.writeWith(Mono.empty());
        };
    }
}

what result do you expect? What is the error message actually seen?

Please help me to see what the problem is in the process of dealing with it. How should it be corrected? Thank you very much!


encounters the same problem, and there is no train of thought at present, but the body parameter of post was fetched yesterday, and today's code cannot be taken without changes. The request is exactly the same.


if you need to get the body, first and then forward it, you should not separate these two steps.
you can try to write something like this:

  http://blog.51cto.com/thinklili

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e46a39-43889.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e46a39-43889.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?