Sending JSON

The previous example sends plain text, if you wish send JSON you can simply pass the object you wish to encode as JSON, whether that be a map or a POJO. As long as Jackson is able to encode it.

For example, the Message class from the previous section, you can create an instance and pass it to the POST method:

Sending a JSON body

  1. Flowable<HttpResponse<Message>> call = client.exchange(
  2. POST("/greet", new Message("Hello John")), (1)
  3. Message.class (2)
  4. );

Sending a JSON body

  1. Flowable<HttpResponse<Message>> call = client.exchange(
  2. POST("/greet", new Message("Hello John")), (1)
  3. Message.class (2)
  4. )

Sending a JSON body

  1. val call = client.exchange(
  2. POST("/greet", Message("Hello John")), Message::class.java (2)
  3. )
1And instance of Message is created and passed to the POST method
2The same class is used to decode the response

With the above example the following JSON will be sent as the body of the request:

Resulting JSON

  1. {"text":"Hello John"}

The JSON itself can be customized however you want using Jackson Annotations.