I am using webclient to integrate with external get API
WebClient webClient = WebClient.builder().filter(logRequestAndResponse()).baseUrl("url").build();
return webClient.get()
.uri(uriBuilder -> uriBuilder.path("path").queryParams(validatedRequestParams).build())
.headers(headers -> { headers.addAll(httpHeaders); })
.retrieve()
.toEntity(ResponseClass.class)
.block();
public ExchangeFilterFunction logRequestAndResponse() {
AtomicReference<String> startTime = new AtomicReference<>();
ExchangeFilterFunction logRequest = ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
log.info("##### Request Body: {}", ObjectMapperHelper.mapToString(clientRequest.body()));
return Mono.just(clientRequest);
});
return logRequest.andThen((clientRequest, next) -> next.exchange(clientRequest)
.flatMap(clientResponse -> clientResponse.bodyToMono(String.class)
.defaultIfEmpty("")
.flatMap(body -> {
log.info("###response in: {}", body);
return Mono.just(clientResponse.mutate().body(body).build());
})
)
The issue is this always results in an empty body payload {} even though no body is explicitly added, which results in receiving an error as the external API is not expecting a body for the get request.
I tried to remove the content-length header, setting it to 0, but I got the same error. How can I prevent an empty payload from being inserted for the get request?