Search code examples
spring-bootkotlinwebclientspring-webflux

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


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

val body = "{\n" +
            "\"email\":\"test@mail.com\",\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.


Solution

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