Found 161 Articles for Rest Assured

How to handle responses in text format in Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 13:09:42

1K+ Views

We can handle responses in text format in Rest Assured. For this, we need to configure Rest Assured such that it can grasp a plain/text type Response. We need to use the registerParser method which is a part of the RestAssured class. Then pass text/plain and Parser.Text as parameters to the registerParser method.We shall first send a GET request via Postman on a mock API URL and then observed its Response.Using Rest Assured, we shall obtain the Response body - Tutorialspoint in text format.ExampleCode Implementationimport org.testng.annotations.Test; import static io.restassured.RestAssured.given; import io.restassured.RestAssured; import io.restassured.parsing.Parser; import io.restassured.response.Response; public class NewTest {   ... Read More

How to handle static JSON in Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 13:04:39

4K+ Views

We can handle static JSON in Rest Assured. This can be done by storing the entire JSON request in an external file. First, the contents of the file should be converted to String.Then we should read the file content and convert it to Byte data type. Once the entire data is converted to Byte, we should finally convert it to string. We shall utilize an external JSON file as a payload for executing a POST request.Let us create a JSON file, say payLoad.json, and add a request body in the below JSON format. This is created within the project.{   ... Read More

How to use TestNG data providers for parameterization in Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 13:00:26

865 Views

We can use TestNG data providers for parameterization in Rest Assured. Using data providers we can execute a single test case in multiple runs. To know more about TestNG data providers visit the below link −https://www.tutorialspoint.com/testng/testng_parameterized_test.htmThis technique can be used for dynamic payloads. For this, we shall create a Java class containing the payload.Then in the second Java class (having the implementation of the POST request), we shall pass the dynamic fields of the payload as parameters to the request body.Please find the project structure for the implementation below.ExampleCode Implementation in NewTest.javaimport org.testng.annotations.DataProvider; import org.testng.annotations.Test; import static io.restassured.RestAssured.*; import io.restassured.RestAssured; ... Read More

How to get JSON fields(nodes) based on conditions using Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 12:52:07

2K+ Views

We can get JSON fields(nodes) based on conditions using Rest Assured. First, we shall obtain a Response body which is in JSON format from a request. Then convert it to string.This is done with the help of the JSONPath class. To parse a JSON response, we have to first convert the response into a string.To obtain the response we need to use the methods - Response.body or Response.getBody. Both these methods are a part of the Response interface.Once a Response is obtained it is converted to string with the help of the asString method. This method is a part of ... Read More

How to iterate through and access the JSON array elements using Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 12:46:27

9K+ Views

We can iterate through and access the JSON array elements using Rest Assured. First, we shall obtain a Response body which is in JSON format from a request. Then convert it to string.To obtain JSON array size, we have to use the size method on the JSON array. Then introduce a loop that shall iterate up to the array size. We shall send a GET request via Postman on a mock API, and observe the Response.Using Rest Assured, let us get the value of the Location field having the values State and zip. They are a part of the JSON ... Read More

How to get a JSON field in a nested JSON using Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 12:41:22

6K+ Views

We can get a JSON field in a complex nested JSON using Rest Assured. First, we shall obtain a Response body which is in JSON format from a request. Then convert it to string.We shall send a GET request via Postman on a mock API URL and observe its Response.Using Rest Assured, let us get the value of the Price field having the value as $150. It is part of the Items. We shall get the value of the Item Count field by traversing the path - Items.Price.ExampleCode Implementationimport static io.restassured.RestAssured.given; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; public ... Read More

How to get a JSON array field in a nested JSON using Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 12:36:40

4K+ Views

We can get a JSON array field in a nested JSON using Rest Assured. First, we shall obtain a Response body which is in JSON format from a request. Then convert it to string.Finally, to obtain a particular array value, we shall use the array index followed by the field name. We shall send a GET request via Postman on a mock API, and observe the Response.Using Rest Assured, let us get the value of the second zip field having the value as 49086. It is part of the second JSON in the Location array. We shall get the value ... Read More

How to get the size of an array within a nested JSON in Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 12:32:22

3K+ Views

We can get the size of an array within a nested JSON in Rest Assured. First, we shall obtain a Response body which is in JSON format from a request. Then convert it to string.Finally, to obtain JSON array size, we have to use the size method. We shall send a GET request via Postman on a mock API, and observe the Response.Using Rest Assured, let us get the size of the Location array within the nested JSON response. The size should be three since it contains information about three locations - Michigan, Indiana, and New York.ExampleCode Implementationimport static io.restassured.RestAssured.given; ... Read More

How to incorporate TestNG assertions in validating Response in Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 12:28:58

937 Views

We can incorporate TestNG assertions in validating Response in Rest Assured. To work with TestNG we have to add the below dependency in the pom.xml in our Maven project. The link to this dependency is available in the below link −https://mvnrepository.com/artifact/org.testng/testngTo verify a response with a TestNG assertion, we need to use the methods of the Assert class. We shall first send a GET request via Postman on a mock API URL and go through the response.ExampleUsing Rest Assured and TestNG, we shall verify the value of the Course field which is Automation Testing.Code Implementationimport org.testng.Assert; import org.testng.annotations.Test; import static ... Read More

How to update the value of a field in a request using Rest Assured?

Debomita Bhattacharjee
Updated on 17-Nov-2021 12:24:14

1K+ Views

We can update the value of a field in a request using Rest Assured. This can be achieved with the help of the PUT request. A PUT request is used to pass data to the server for the modification of a resource. The difference between POST and PUT is that POST request is not idempotent.This means invoking the same PUT request numerous times will always yield the same output. But invoking the same POST request numerous times will create a similar resource more than one time.Let us send a GET request on an API and observe its response using Postman.Using ... Read More

Advertisements