title: How do you read response body in Spring Cloud Gateway (Webflux) POST filter
body: <p>Spring Cloud Gateway documents a post filter phase like this: <a href="https://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/single/spring-cloud.html#_writing_custom_gatewayfilter_factories" rel="nofollow noreferrer">https://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/single/spring-cloud.html#_writing_custom_gatewayfilter_factories</a></p>
  
  <p>I've implemented a filter that has nothing in pre filter phase, but I would like to manipulate the response body in post filter phase before committing it to the client waiting the response to succeed. </p>
  
  <p>I've managed to successfully alter the response but I have found no solution to how actually read the original response from the successful proxied request. I think I have tried almost everything and I know it's probably not the best practice to read the body in the middle of webflux flow as it sort of defeats the whole purpose. But it drives me nuts it's that hard to just read the data from the response.</p>
  
  <p>My filter looks like this:</p>
  
  <pre><code>class PostFilter : AbstractGatewayFilterFactory&lt;PostFilter.Config&gt;(Config::class.java) {
      @Autowired
      private lateinit var tokenService: TokenService
  
      /* This is required when implementing AbstractGatewayFilterFactory.
       * With config we could pass filter configurations in application.yml route config
       * This can be used if it is needed to parametrise filter functionality
       */
      class Config
  
      override fun apply(config: Config?): GatewayFilter {
          return GatewayFilter { exchange: ServerWebExchange, chain: GatewayFilterChain -&gt;
  
              chain.filter(exchange).then(Mono.defer {
                  val response = exchange.response
                  val bytes: ByteArray = "Some text".toByteArray(StandardCharsets.UTF_8)
                  val buffer: DataBuffer = exchange.response.bufferFactory().wrap(bytes)
                  response.headers.contentLength = bytes.size.toLong()
  
                  // How do I get the original response?
                  response.writeWith(Flux.just(buffer))
              })
  
  
          }
      }
  }
  </code></pre>
  
  <p>I found this example: <a href="https://github.com/spring-cloud/spring-cloud-gateway/blob/master/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/WebClientWriteResponseFilter.java" rel="nofollow noreferrer">https://github.com/spring-cloud/spring-cloud-gateway/blob/master/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/WebClientWriteResponseFilter.java</a> where I see <code>ClientResponse clientResponse = exchange.getAttribute(CLIENT_RESPONSE_ATTR);</code> but when I try it, it doesn't contain a <code>ClientResponse</code> object that could be used to fetch the body. Instead I get netty's <code>HttpClientResponse</code> object that has no way to get hold of the response body.</p>
  
  <p>I've searched numerous stackoverflow topics covering problems that look a bit similar but none of them are actually the same.</p>
  
  <p>I also tried this approach as in this <a href="https://github.com/spring-cloud/spring-cloud-gateway/issues/177#issuecomment-361411981" rel="nofollow noreferrer">https://github.com/spring-cloud/spring-cloud-gateway/issues/177#issuecomment-361411981</a> where you can use <code>ServerHttpResponseDecorator</code> to read the body. However, it only reads the body if the <code>writeWith()</code> method is called. So if I want the original response, I would have to get the original response as a <code>DataBuffer</code> to write it again so the writeWith would be triggered. But if I had is as <code>DataBuffer</code> I wouldn't need the whole <code>ServerHttpResponseDecorator</code> at all.</p>
  
  <p>So please, help me - I've banged my head against the wall and feel like giving up on this. I usually never give up, since I feel this should be even trivial to do because it's such a simple thing: "read the response body after proxied request". </p>
  
  <p>Any ideas?</p>