Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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\":\"[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.

like image 331
Avv Avatar asked Nov 09 '18 12:11

Avv


People also ask

How do I pass a query parameter in WebClient?

In order to pass single value query parameters, create a path of the base resource URI and then use queryParam() method to append key value pairs.

What is Web client in spring boot?

In simple words, the Spring WebClient is a component that is used to make HTTP calls to other services. It is part of Spring's web reactive framework, helps building reactive and non-blocking applications. To make HTTP requests, you might have used Spring Rest Template, which was simple and always blocking web client.


1 Answers

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

like image 79
Brian Clozel Avatar answered Sep 18 '22 19:09

Brian Clozel