Send GET request using REST-Assured

This entry is part 7 of 14 in the series REST Assured Tutorial
How to send GET request using REST-Assured and validate the response.

GET is used to request a resource from the REST API. Your REST API is supposed to send the response back when you send the GET request. Before using REST-Assured for sending request and validating the data, we must be aware about the URI and the type of data it will accept and send back. While testing an enterprise application, your development team will provide you this information. But, for this training purpose you can use Postman tool to analyse the request and response. Postman is very simple tool for sending the API request. Postman is chrome extension, you can get the chrome extension from Chrome Store here or if you want an independent desktop version please download postman from here.

Please note we are using Postman only because it will let you see the details of a request and response very clearly, we are not using it for testing. If you want to use any other tool instead of postman, you can use anything.

No more theory now, in this tutorial we will get our hands dirty with our REST-Assured code. Now simply paste the below URL in postman like in a screenshot below :

Request URI : https://postman-echo.com/GET

postman_getRequest

Please make sure you select GET method from the dropdown in Postman.

Request URI : https://postman-echo.com/GET

Now Click the Send button :

postman_sendGET_request

You will see a response in JSON format like below. If you get any errors or dont receive any response, please verify the URI or press the Send button again. Sometimes Postman is unable to make an HTTP call.

postman_get_response

Response Body :

Response Header :

Now that we know how our response looks like, we will do the same thing using REST-Assured.

How to send GET using REST-Assured in Eclipse :

Open the project which we created in our previous tutorials.
Right Click on the project – New – Class. Provide the class name TestGetMethod and create your class as below.

@Test is TestNG annotation, which defines a function as an independent test. Here we have written three different tests. Priority of the execution can be set using priority parameter in @Test annotation. This means our three test will be executed in the same sequence they are written in.

  • The first @Test will verify the Status Code in the response. Status code is a numerical number which provides more information about the response we receive from the server. Status code 200 denotes that there was no problem with the server, we received a response successfully.
  • The second @Test will verify response body. We are validating if the host is equal to “postman-echo.com”  or not.
  • Third @Test will verify response Header, we are validating if the encoding format supports gzip or not.

Run the class now. Congratulations you successfully sent the GET request, received the data and validated it. You will see the result for your three different test in TestNG.

Nore : equalTo() and hasItems() are hamcrest matchers, you need to statically import from org.hamcrest.Matchers.

 

Please provide your views, suggestions, errors in the code, any issues if you are facing, below in the comment section. Our team will be glad to help you.

Series Navigation<< Introduction to HTTP methodsSend POST request using REST-Assured >>