How to intercept Spring WebFlux WebClient request and response with headers and bodies?

394 Views Asked by At

I'm working on a Spring Boot application that uses Spring WebFlux, and I'm trying to intercept WebClient requests and responses. Specifically, I need to capture both the headers and the request/response bodies to compute and verify an HMAC.

I've explored the use of ExchangeFilterFunction, which allows me to access ClientHttpRequest and ClientHttpResponse and retrieve the headers. However, I'm facing an issue with capturing the request/response bodies using this approach.

On a ExchangeFilterFuction, the request.body() is a BodyInsert, is there any way to extract the body from there? Also on the filter, the response.bodyToMono(String.class) doesn't retrieve the response body.

On the other hand, I've looked into using Encoder and Decoder, which can help me retrieve the body but do not provide access to the Request/Response objects, making it challenging to obtain the header information.

Have anyone manage to achieve anything related to this?

0

There are 0 best solutions below