.body("{\"id\":\"1234ABC\", \"userId\" :\"user1\", \"title\":\"This is a title\", \"body\":\"This is the body\"}")

.asJson();

System.out.println("statusCode = "+response.getStatus());

System.out.println("body = "+gson.toJson(response.getBody()));

}catch(UnirestExceptione){

//Handle exception

}

As you can see from the example above, the API is very readable and fluent. The request is executed when you call asJson(). Other methods include asObject(), asString() and asBinary() depending on your response type.

Example 2:

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Unirest.put("https://jsonplaceholder.typicode.com/posts/1")

.header("Accept","application/json")

.header("Content-Type","application/json")

.body("{\"id\":\"1234ABC\", \"userId\" :\"user1\", \"title\":\"This is a title\", \"body\":\"This is the body\"}")

.asJsonAsync(newCallback<JsonNode>(){

@Override

publicvoidcompleted(HttpResponse<JsonNode>response){

System.out.println("statusCode = "+response.getStatus());

System.out.println("body = "+gson.toJson(response.getBody()));

}

@Override

publicvoidfailed(UnirestExceptione){

//handle error

}

@Override

publicvoidcancelled(){

//handle cancelled

}

});

This second example, is very similar to the first but you can pass a callback object to the asJson() method. This allows it you process the response asynchronously.

In summary, I hope you found this post useful and it helps you in your development.