3

I am using ExchangeFilterFunction to log requests and responses but I don't know how I can get the request/response body as a string or JSON format and log it.

The code looks as follows:

//logging method, url and headers, but the body of the request is needed

fun logClientRequest(): ExchangeFilterFunction {
    return ExchangeFilterFunction.ofRequestProcessor() {

        logger.info("Client CRM Request: {} {}", it.method(), it.url());

        //TODO: LOG HERE BODY REQUEST

        it.headers().forEach { name, values ->
            run {
                logger.info("Headers:")
                values.forEach { value -> logger.info("{}={}", name, value) }
            }
        }
        Mono.just(it)
    }
}

fun logClientResponse(): ExchangeFilterFunction {
    return ExchangeFilterFunction.ofResponseProcessor() {
        logger.info("Client CRM Response {}", it.bodyToFlux(Any::class.java))
        Mono.just(it)
    }
}

The bodyToFlux in logClientResponse() logs only:

ExchangeFilterFunction Client CRM Response checkpoint("Body from PUT https:myurl [DefaultClientResponse]")
D.B
  • 4,009
  • 14
  • 46
  • 83
  • 1
    Does this answer your question? [How to log request and response bodies in Spring WebFlux](https://stackoverflow.com/questions/45240005/how-to-log-request-and-response-bodies-in-spring-webflux) – Toerktumlare Oct 24 '20 at 19:12
  • 1
    Or this: [how to log Spring 5 WebClient call](https://stackoverflow.com/q/46154994/4110190) – Jonas Pedersen Aug 06 '21 at 11:39

0 Answers0