GET Request using Java 11 HttpClient API

In this article, we are going to see how to perform a GET call with new Java 11 HttpClient API. This API is available in the standard libraries package java.net . Earlier we used to use external API’s like Apache HttpClient to make the HttpRequests but now we longer required to use any other external library like Apache. Let’s learn “How to send a GET request with Java 11 HttpClient API?”

Sync_ASync_Get_HttpClient_Request_Java11_Featured_Image_Techndeck

 

GET Request using Java 11 HttpClient API...!!! Click To Tweet

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

  1. What is HTTP Get Request?
  2. How to send Sync GET request using HttpClient?
  3. How to send Async GET request using 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 send Sync GET request using HttpClient?

This new HttpClient API can be used to make the HTTP Requests synchronously as well as asynchronously. A synchronous request is nothing but blocks the current thread and wait until the response is available.

In this example, 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 Sync GET request to the above mentioned Service Endpoint:

 

Let’s try to understand the code:

1. Specify the URL

 

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

 

3. Create a new HttpClient object

 

4. Submit the Sync GET Request with BodyHandler which defines the response body should be of string format, and store the output in the response object

 

5. Finally, extract the status code and response body using the response object created above

 

Output:

 

3. How to send ASync GET request using HttpClient?

Here, We are going to perform the the same request as above but this time ‘Asynchronous’. For the synchronous call, we used ‘send’ method in the above example. While, for the Asynchronous call, we need to use sendAsync method which won’t block the current thread and returns a CompletableFuture to build asynchronous operation pipeline.

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

 

Let’s try to understand the code:

1. Specify the URL

 

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

 

3. Create a new HttpClient object

 

4. Submit the ASync GET Request with response BodyHandlers as a string, finally apply the stage to get response body and store the output in the CompletableFuture object

 

5. Finally, extract the response body using the response object created above

 

Output:

That’s it, it’s that simple to make a Sync & ASync GET Requests using HttpClient library. It’s a very valuable addition as part of Java 11. 

 

GET Request using Java 11 HttpClient API...!!! Click To Tweet

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

Other Useful References:

Leave a Reply

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

Facebook Twitter Instagram YouTube