I want to access an HTTP API which provides a DELETE
endpoint. This particular endpoint expects a list of items (which I want to delete) as JSON body.
Now, my problem is, I’m using Spring Webflux. But its WebClient doesn’t give me the possibility, to send a body with a DELETE
request. For a POST
, I’d do this:
webClient.post() .uri("/foo/bar") .body(...) .exchange()
But for DELETE
, I get a RequestHeadersSpec which doesn’t give me the option to provide a body(...)
:
webClient.delete() .uri("/foo/bar") .body(...) <--- METHOD DOES NOT EXIST .exchange()
So, what’s the way to achieve this with Spring Webflux on the client side?
Advertisement
Answer
You can use webClient’s method()
operator. Simple example,
return webClient .method(HttpMethod.DELETE) .uri("/delete") .body(BodyInserters.fromProducer(Mono.just(new JSONObject().put("body","stringBody").toString()), String.class)) .exchange()