To make a GET request using Unirest in Java, you will need to include the Unirest library in your project. Here is an example of how to make a GET request and handle the response:
import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.JsonNode; import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.exceptions.UnirestException;
get method of the Unirest class and pass in the URL of the endpoint you want to call:HttpResponse<JsonNode> response = Unirest.get("http://example.com/endpoint")
.asJson();
int statusCode = response.getStatus();
if (statusCode == 200) {
// Request was successful
} else {
// Handle error
}
getBody method:JsonNode body = response.getBody();
queryString or header methods:HttpResponse<JsonNode> response = Unirest.get("http://example.com/endpoint")
.queryString("param1", "value1")
.header("Accept", "application/json")
.asJson();