In this article, we are going to see a step by step process on how to perform a GET call with Apache HttpClient library. Here, we are going to use HttpClient Version 4.5 to make the request. HttpClient supports all HTTP methods: GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE. All these methods are part of the HTTP/1.1 specification and for each method, there we have unique classes available in the library like HttpGet, HttpPost, HttpPut, HttpDelete, HttpHead, HttpOptions and HttpTrace. In this example, we will see “How to send a GET request with Apache HttpClient by using HttpGet method?”

Get_Request_HttpClient_Techndeck

 

Simple example of GET Request using Apache HttpClient...!!! Click To Tweet

In this tutorial, we are going to cover below topics:

  1. What is HTTP Get Request?
  2. How to configure HttpClient library in your project?
  3. How to send GET request using Apache HttpClient?

 

Check out: GET REQUEST using another popular API testing Framework – REST ASSURED

Let’s begin:

1. What is HTTP GET Request?

GET method is one of the most common method of HTTP Protocol which is used to request data from a specific resource.

Some key points of GET requests:

  • GET requests parameters remain in the browser history because they have been sent as part of the URL
  • GET requests can only be used to retrieve data not to modify
  • GET requests can be cached
  • GET requests are less secure and should be avoided when trying to retrieve data from a sensitive resource
  • GET requests parameter data is limited as there are lengh restrictions
  • GET requests can be bookmarked
  • GET requests only allow ASCII characters
  • GET requests are prone to get hacked easily

 

2. How to configure HttpClient library?

In order to use HttpClient support, you would first need to add it’s dependency into your project. You can add the dependency into your maven or gradle build files. 

As we are using a Maven based project and version 4.5.9, In order to use the same, you can copy the below dependency into your pom.xml file.

You can check the below link to get the different versions of HttpClient library. 

https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient

Or

In case, you are not using maven or gradle or any other build mechanism in your project, then download the HttpClient jar file from this location and configure it into your classpath. 

3. How to send GET request using Apache HttpClient?

In this tutorial, we will test the ‘Dummy Sample Rest API’ which is available here. This page contains Fake Online REST API for the testing purpose which are performing various CRUD operations.

Let’s take an example of one of the API GET endpoint available at the above-mentioned website which is ‘/employees’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/employees‘.

At the above resource URL, information about all the employees is present and now we are trying to access those employees details in this below example like id, employee_name, employee_age, employee_salary and profile_image.

Here is the code to send the GET request to the above mentioned Service Endpoint:

 

Let’s try to understand the code:

1. Specify the URL and set up CloseableHttpClient object

 

2. Create a basic Get Request using HttpRequest object and pass the resource URI to it

 

3. Submit the Request using HttpGet -> Execute method

 

4. Create a BufferedReader object and store the raw Response content into it.

 

5. Throw runtime exception if status code isn’t 200

 

6. Create the StringBuffer object and store the response into it.

 

7. Finally, Lets validate if a text ’employee_salary’ is present in the response

 

That’s it, it’s that simple to make a GET method Request using Apache HttpClient 🙂

 

Simple example of GET Request using Apache HttpClient...!!! Click To Tweet

If you like this post , please check out my other useful blog posts on Rest Assured:

Other Useful References: