Get Requests
GET requests to retrieve information from the server or from a specific resource, which can be either a user or a product, or an article, get() method is used to make a GET request. The below code shows how to make a GET request to retrieve information about the specific user:
given().
baseUri("http://api.example.com").
port(8080).
when().
get("/users/1").
then().
// ...
In the above example, the get(“/user/1”) method makes a GET request to the URL “http://api.example.com:8080/users/1”. then() method can be used to verify the response, like checking the status code, the response body, or the response headers.
Making GET Requests with REST Assured
Making GET requests is simple, and the below code shows how to make a GET request to retrieve information about specific users:
given().
baseUri("http://api.example.com").
port(8080).
when().
get("/users/1").
then().
statusCode(200).
body("id", equalTo(1)).
body("name", equalTo("vivek")).
header("Content-Type", equalTo("application/json")).
In this example, the get("/users/1") method makes a GET request to the URL “http://api.example.com:8080/users/1”. The then() method is used to verify the response, such as checking the status code, the response body, and
the response headers.
The statusCode(200) method verifies that the HTTP status code of the response is 200 (OK). The body("id", equalTo(1)) method verifies that the "id" field in the response body is equal to 1. The body("name", equaITo("vivek")) method verifies that the "name" field in the response body is equal to "vivek". The header("Content-Type", equalTo("application/json")) method verifies that the "Content-Type" header in the response is equal to "application/json".
This is a basic example of making a GET request with REST Assured. REST
Assured provides many more features and options for making GET requests, such as support for query parameters, headers, and cookies. It is a powerful tool for testing RESTful web services and can be easily integrated into your existing testing workflow.
Assertions
To verify the behavior and output of the application, Assertions are used; in addition, they are used to verify the response of a REST API, such as verifying the response status code, response body, and response headers. For example: using statuscode(), you can verify the status code of a response.
given().
baseUri("http://api.example.com").
port(8080).
when().
get("/users/1").
then().
statusCode(200).
In this example, the statusCode(200) method verifies that the HTTP status code of the response is 200 (OK). If the status code is not 200, the test will fail.
REST Assured provides a wide range of assertions for verifying the response of a REST API, making it a powerful tool for testing RESTful web services. By using assertions, you can ensure that the API behaves as expected and that the data returned by the API is accurate and valid.
Making Assertions with REST Assured
In REST Assured, then() method is used to make assertions. It verifies the response of REST API to make the assertions.
given().
baseUri("http://api.example.com").
port(8080).
when().
get("/users/1").
then().
statusCode(200).
In this example, the statusCode(200) method verifies that the HTTP status code of the response is 200 (OK). If the status code is not 200, the test will fail.
REST also provides methods for verifying the response body and header, respectively. You can use the body() and header() methods for that purpose.
given().
baseUri("http://api.example.com").
port(8080).
when().
get("/users/1").
then().
statusCode(200).
body("id", equalTo(1)).
body("name", equalTo("vivek")).
In this example, the body("id", equaITo(1)) method verifies that the "id" field in the response body is equal to 1. The body("name", equalTo("vivek")) method verifies that the "name" field in the response body is equal to "vivek".
Types of Assertions in REST Assured
Various assertions are used to verify the response of REST APIs. Some of them include
-
Status code assertions: Verify the HTTP status code of the response. statuscode() method can be used to verify the status code is 200(OK).
-
Body assertions: Verify the contents of the response body. For example, the body() method.
-
Header assertions: Verify the contents of the response headers. For example, the header() method.
-
Time-related assertions: Verify the time taken for the response to be returned. For example, the time() method.
-
JSON and XML assertions: verify the content of JSON and XML response. For example, jsonPath() and xmlPath().
Frequently Asked Questions
What is REST Assured?
It is a JAVA library for testing REST APIs and provides a simple and intuitive interface for testing REST APIs and making assertions about the responses received.
What are the key features of REST Assured?
It includes the ability to set a default host and port, make GET requests, and assertions about the responses received.
What is a GET request in REST Assured?
These are requests made using the HTTP GET method to retrieve information from a REST API. Provides a Simple interface for making requests and examining responses.
What are assertions in REST Assured?
These statements tell the expected behavior of REST API and are used for validating responses from REST APIs.
Conclusions
In the blog, we discussed the default host and port concept and how to set them using the baseUri() and port() methods. The benefit of using a default host and port include consistency and ease of use. Basics of GET request and how to make GET request using the when() and get(). The importance of understanding the response code in GET requests and how to verify the response's status code using the statuscode() method. The importance of making assertions to verify the response of REST API. In a nutshell, we have discussed the features and functionalities of REST Assured and how to use them for testing REST APIs.
You can also refer to the below articles:
You can learn the Basics of Java and data structures and algorithms in Java on Coding Ninjas. Refer to our guided path on code studio to learn more about DSA Competitive Programming, Javascript, System Design, etc. Enroll in our courses and refer to the mock test and Problems available. Also, look at the Interview Experiences for placement preparations.
Happy Learning, Ninjas.