So far, we have already covered the basics of Rest API Testing using Rest Assured. If you haven’t go through the basics, please check my previous tutorial. It would basically provide a glimpse of how to perform Rest API testing. In this example, we will see “How to send a GET request to a Rest API endpoint using Rest-Assured”.

Simple example of GET Request using REST Assured...!!! Share on X

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

  1. What is Get Request?
  2. How to send a GET request using Rest Assured?
  3. How to validate the Response?

Check out: GET REQUEST using Apache HttpClient in JAVA

Let’s begin:

1. What is GET Request?

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

Some key points of GET requests:

  • GET requests parameters to 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 length restrictions
  • GET requests can be bookmarked
  • GET requests only allow ASCII characters
  • GET requests are prone to get hacked easily

2. How to send a GET request using Rest Assured?

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

Let’s take an example of one of the API GET endpoints available at the above-mentioned website which is ‘/unknown/2’. The full-service URL with the endpoint is ‘https://reqres.in/api/unknown/2‘.

At the above resource URL, information about a specific resource is present and now we are trying to access it’s details in this below example like id, name, color etc.

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

Let’s try to understand the code:

1. Setting up Base URI

Base URI is the root address of the Resource. And, by this particular line of code, we are specifying to REST assured to use “reqres.in/api” as the root URL of the service.

2. Specifying the exact resource to look for and make a GET request to that resource

Here, using this code, we are looking to make a GET request to an exact resource which is “/unknown/2” in this case. Hence, the complete Service Endpoint would be “http://reqres.in/api/unknown/2” to which we are sending a GET request. And, the output of the GET call would be stored in the REST Assured ‘Response’ object.

3. Response validation

a. response.asString() : It displays the response in a string format

b. response.getStatusCode() : This line of code would extract the status code from the response.

c. response.asString().contains(“#C74375”) : This line of code helps to check if the string ‘#C74375’ present in the response or not.

d. assertEquals(200, response.getStatusCode()) : This would throw true or false based on the condition if status code from the response matches with the value 200 or not.

e. assertEquals(“fuchsia rose”, response.jsonPath().getString(“data.name”)) : This would throw true or false based on the condition if the value of the element ‘data.name’ from the response matches with the value ‘fuchsia rose’ or not.

3. How to validate the Response?

I’ve already explained the validation above. Now, Let’s just execute the above Test class (RestAssured_GetRequest) and verify the output:

a. Check for the Response i.e {“data”:{“id”:2,”name”:”fuchsia rose”,”year”:2001,”color”:”#C74375″,”pantone_value”:”17-2031″},”support”:{“url”:”https://reqres.in/#support-heading”,”text”:”To keep ReqRes free, contributions towards server costs are appreciated!”}}

b. Check for the Status Code i.e ‘200’ as intended.

c. Check if the ‘#C74375’ string present in the response i.e ‘true’ as intended.

d. Performed the assertEquals verification on Status Code, which is successfully passed. 

e. Lastly, we performed assertEquals verification for a particular element in the response i.e ‘data.name’, It’s value from the response matched the expected value, hence no failure in the output.

Console Output:

That’s it, it’s that simple to make a GET method Request using REST Assured API: ?

Simple example of GET Request using REST Assured...!!! Share on X

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

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak