How to send request body in spring-boot web client?

Avv picture Avv · Nov 9, 2018 · Viewed 27.4k times · Source

I'm facing some problem while sending request body in spring boot web client. Trying to send body like below:

val body = "{\n" +
            "\"email\":\"[email protected]\",\n" +
            "\"id\":1\n" +
            "}"
val response = webClient.post()
    .uri( "test_uri" )
    .accept(MediaType.APPLICATION_JSON)
    .body(BodyInserters.fromObject(body))
    .exchange()
    .block()

Its not working. Request body should be in JSON format. Please let me know where I'm doing wrong.

Answer

Brian Clozel picture Brian Clozel · Nov 9, 2018

You're not setting the "Content-Type" request header, so you need to append .contentType(MediaType.APPLICATION_JSON) to the request building part.