I want to pass the generic request body while making API call through WebClient. I have dynamic key-value pairs in the database like (key1-value1, key2-value2, key3-value3). This key-value may grow or shrink.
Is there any way to call API with dynamic data with the JSON request body?
webClient.post().uri(uri).header(CONTENT_TYPE, APPLICATION_JSON)
.body({DYANAMIC JSON}).retrieve().onStatus(HttpStatus::isError, clientResponse -> {
return Mono.error(new Exception("error"));
}).bodyToMono(String.class);
Thank you
You can just pass the body as map and in body you can map it to either Map.class or Object class. Based on your requirement you can pass JsonObject as well.
Map<String, String> r = new HashMap<>();
webClient.post().uri(uri).header(CONTENT_TYPE, APPLICATION_JSON)
.body(Mono.just(r), Map.class).retrieve().onStatus(HttpStatus::isError, clientResponse -> {
return Mono.error(new Exception("error"));
}).bodyToMono(String.class);