title: How to log spring webclient request and response body?
body: <p>added an ExchangeFilterFunction to WebClient which logs request and response but while logging, was unable to log request and response body as a string or JSON. It prints as an object  </p>
  
  <p>Tried different castings and retrieving the body as a string using bodyToMono, toEntity but they return an object and not string exactly.</p>
  
  <pre><code>        logRequest(clientRequest.url(), clientRequest.method(), clientRequest.headers(), clientRequest.body());
          Mono&lt;ClientResponse&gt; response = exchangeFunction.exchange(clientRequest);
          ClientResponse clientResponse = response.block();
  
        logResponse(clientResponse.statusCode(), clientResponse.headers(), clientResponse.toString(), clientResponse);
  
          return response;
      }```
  
  ```private void logRequest(URI uri, HttpMethod method, HttpHeaders httpHeaders, BodyInserter&lt;?, ? super ClientHttpRequest&gt; body) {
  
          log.info("Request: {}", 
                   LogVar.with("Body", body)
          );
      }
  
      private void logResponse(HttpStatus statusCode, Headers headers, String body, ClientResponse extractor) {
          log.info("Response: {}", 
                   , LogVar.with("Body", extractor.bodyToMono(String.class))
          );
      }```
  
  Expecting Json OR XML whatever the request/response is, to be logged as a string.
  </code></pre>