example of how to make a GET request using Unirest in Java

https://‮al.www‬utturi.com
example of how to make a GET request using Unirest in Java

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:

  1. First, you will need to import the necessary libraries:
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;
  1. To make a GET request using Unirest, you can use the 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();
  1. You can then check the status code of the response to see if the request was successful:
int statusCode = response.getStatus();
if (statusCode == 200) {
    // Request was successful
} else {
    // Handle error
}
  1. If the request was successful, you can access the response body using the getBody method:
JsonNode body = response.getBody();
  1. You can also add query parameters or headers to the request by using the queryString or header methods:
HttpResponse<JsonNode> response = Unirest.get("http://example.com/endpoint")
                                        .queryString("param1", "value1")
                                        .header("Accept", "application/json")
                                        .asJson();
Created Time:2017-10-17 22:51:24  Author:lautturi