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?
If a DELETE request includes an entity body, the body is ignored [...] Additionally here is what RFC2616 (HTTP 1.1) has to say in regard to requests: an entity-body is only present when a message-body is present (section 7.2)
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()
return webClient
.method(HttpMethod.DELETE)
.uri(url)
.body(Mono.just(request), requestClass)
.retrieve()
.toEntity(Void.class);
As result, we will get:
Mono<ResponseEntity<Void>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With