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 XIn this tutorial, we are going to cover the below topics:
- What is Get Request?
- How to send a GET request using Rest Assured?
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * GET Request * @author Deepak Verma */ import static org.testng.Assert.assertEquals; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response; public class RestAssured_GetRequest { @Test public void retrieveSpecificResource() { RestAssured.baseURI = "https://reqres.in/api"; Response response = null; try { response = RestAssured.given() .when() .get("/unknown/2"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); System.out.println("Status Code :" + response.getStatusCode()); System.out.println("Does Reponse contains sting: '#C74375'? :" + response.asString().contains("#C74375")); assertEquals(200, response.getStatusCode()); assertEquals("fuchsia rose",response.jsonPath().getString("data.name")); } } |
Let’s try to understand the code:
1. Setting up Base URI
1 | RestAssured.baseURI = "http://reqres.in/api"; |
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
1 2 3 4 | Response response = null; response = RestAssured.given() .when() .get("/unknown/2"); |
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
1 2 3 4 5 6 | System.out.println("Response :" + response.asString()); System.out.println("Status Code :" + response.getStatusCode()); System.out.println("Does Reponse contains sting: '#C74375'? :" + response.asString().contains("#C74375")); assertEquals(200, response.getStatusCode()); assertEquals("fuchsia rose",response.jsonPath().getString("data.name")); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [RemoteTestNG] detected TestNG version 7.0.1 Response :{"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!"}} Status Code :200 Does Reponse contains sting: '#C74375'? :true PASSED: retrieveSpecificResource =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Passes: 1, Failures: 0, Skips: 0 =============================================== |
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 XIf you like this post, please check out my other useful blog posts on Rest Assured:
- How to send a POST Request using Rest Assured
- How to make a GET Request using Apache HttpClient in Java
Other Useful References:
Very useful!. Thanks Deepak
Thank you Zlatina.
Thanks
Hi Deepak,
Why does it have to be put in a try-catch block?
Thank you,
And
Hi And,
It’s not at all required. I follow it as a practice to place the main content in the try catch block to handle the default exception. That’s all.
If you don’t want to use, then it’s fine too.
Thanks
very elaborative. thanks. you’ve saved my time.
Thank you Aman