title: How to read the request body with spring webflux
body: <p>I'm using Spring 5, Netty and Spring webflux to develop and API Gateway. Sometime I want that the request should be stopped by the gateway but I also want to read the body of the request to log it for example and return an error to the client.</p>
  
  <p>I try to do this in a WebFilter by subscribing to the body.</p>
  
  <pre><code>@Override
  public Mono&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) {
      if (enabled) {
          logger.debug("gateway is enabled. The Request is routed.");
          return chain.filter(exchange);
      } else {
          logger.debug("gateway is disabled. A 404 error is returned.");
  
          exchange.getRequest().getBody().subscribe();
          exchange.getResponse().setStatusCode(HttpStatus.NOT_FOUND);
          return exchange.getResponse().writeWith(Mono.just(exchange.getResponse().bufferFactory().allocateBuffer(0)));
      }
  }
  </code></pre>
  
  <p>When I do this it works when the content of the body is small. But when I have a large boby, only the first element of the flux is read so I can't have the entire body. Any idea how to do this ?</p>