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
Flowable<HttpResponse<Message>> call = client.exchange(
POST("/greet", new Message("Hello John")), (1)
Message.class (2)
);
Sending a JSON body
Flowable<HttpResponse<Message>> call = client.exchange(
POST("/greet", new Message("Hello John")), (1)
Message.class (2)
)
Sending a JSON body
val call = client.exchange(
POST("/greet", Message("Hello John")), Message::class.java (2)
)
1 | And instance of Message is created and passed to the POST method |
2 | The 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
{"text":"Hello John"}
The JSON itself can be customized however you want using Jackson Annotations.