Created awaitBodyOrNull function to handle empty body

suspend inline fun <reified T : Any> WebClient.ResponseSpec.awaitBody() : T does not handle nullable body

suspend fun WebClient.ResponseSpec.awaitBodilessEntity() ignores body completely even when the content-length is above zero

Comment From: pivotal-issuemaster

@ValentinShakhov Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: pivotal-issuemaster

@ValentinShakhov Thank you for signing the Contributor License Agreement!

Comment From: rstoyanchev

This looks like a duplicate of #26491, unless I'm mistaken @sdeleuze?

Comment From: valentin-apc

awaitBody() is not something that I wanted to achieve here. I am accessing an endpoint that might or might not return content, therefore I need to be able to handle both empty and non-empty body. And the return type expected is a specific nullable one that I can declare with a particular class (not Any). My current workaround is like this:

suspend fun get() : SomeDto? = webClient.get().retrieve().bodyToMono(SomeDto::class.java).awaitSingleOrNull()

Maybe I am missing or misusing something?

Comment From: sdeleuze

After a quick first look, it seems a valid proposal. The use case makes sense and we provide already such variant for ClientResponse but not for WebClient.ResponseSpec.

Comment From: valentin-apc

After a quick first look, it seems a valid proposal. The use case makes sense and we provide already such variant for ClientResponse but not for WebClient.ResponseSpec.

Could you please provide an example of doing that with a ClientResponse? I did try it as well, but it would just hang and wait for the response forever. It could be that I did it wrong, though. I would appreciate that.

p.s.: sorry, I am using different github accounts :)

Comment From: sdeleuze

For ClientResponse, we probably miss an awaitExchangeOrNull() variant to use it with awaitBodyOrNull(). Could you please submit a PR or create a related issue ?