Execute HTTP GET Request in Java

In this tutorial, you will learn how to use the HTTP client to execute an HTTP GET request in Java.

HTTP GET request is used to retrieve and request data from a specified resource in a server. Requests using the GET HTTP method should be Idempotent which means that executing it should not have any side effects.

If you are learning how to send HTTP methods in Java, then you might also be interested to learn in the following tutorials:

To execute an HTTP request in Java, we need to have an HTTP client as a dependency. In this tutorial, we will cover the HTTP GET Request using the Apache HttpClient.

First, we need to add Maven dependency:

<dependency>
    <groupid>org.apache.httpcomponents</groupid>
    <artifactid>httpclient</artifactid>
    <version>4.5.13</version>
</dependency>


Find other versions here → Apache HTTP Client. If you are not using Maven, you can download JAR from the location above.

Execute a GET request in Java using the Apache HTTP Client

Below is a simple example of executing an HTTP GET request in Java:

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

public class Test {

  public static void main(String[] args) throws IOException {

    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
      HttpGet httpget = new HttpGet("https://jsonplaceholder.typicode.com/todos/1");

      System.out.println("Executing GET request...");
      HttpResponse response = httpclient.execute(httpget);
      String responseBody = new BasicResponseHandler().handleResponse(response);

      System.out.println("Response: " + responseBody);
    }

  }
}
Output: Executing GET request… Response: { “userId”: 1, “id”: 1, “title”: “delectus aut autem”, “completed”: false }
 
Here we used the try-with-resources to create an HttpClient. That means we don’t have to close it after executing the request. The try-with-resources statement ensures that each resource is closed at the end of the statement.
 
We also used the BasicResponseHandler to retrieve the response body.

Execute a GET request with HTTP headers

We can also add HTTP headers to the request, like in the following example:

import org.apache.http.HttpResponse;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

public class Test {

  public static void main(String[] args) throws IOException {

    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
      HttpUriRequest request = RequestBuilder.get()
              .setUri("https://jsonplaceholder.typicode.com/todos/1")
              .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
              .setHeader("Authorization", "Bearer 123token")
              .build();

      System.out.println("Executing GET request...");
      HttpResponse response = httpclient.execute(request);

      String responseString = new BasicResponseHandler().handleResponse(response);

      System.out.println("Response: " + responseString);
    }
  }
}
Output: Executing GET request… Response: { “userId”: 1, “id”: 1, “title”: “delectus aut autem”, “completed”: false }
 
Continue to the next lesson to learn how to execute HTTP POST request in Java.
 
Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *