In this tutorial, you will learn how to send HTTP Requests using Rest Assured API Testing Library. We are going to cover all the Request types in this Rest Assured Examples article – GET, POST, PUT, PATCH and DELETE.
Rest Assured Examples - GET, POST, PUT, PATCH and DELETE...!!! Share on X
Rest Assured Library Dependency Configuration:
Add the below-mentioned dependency to your maven project’s pom.xml.
1 2 3 4 5 6 | <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>3.3.0</version> <scope>test</scope> </dependency> |
Note: you can choose the ‘version’ of your choice.
Now, we are all set to get into the REST-assured HTTP API Requests.
In the examples below, 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 we are going to use for various CRUD operations.
In this Article, we are going to cover:
GET REQUEST
The HTTP GET request is used to request a resource from the server. In the example below, we are going to use get() method of Rest Assured Java Library.
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 | /** * GET Request * @author Deepak Verma */ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.response.Response; public class RestAssured_GET_Example { @Test public void getExample() { RestAssured.baseURI = "https://reqres.in/api"; Response response = null; try { response = RestAssured.given() .when() .get("/users/2"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); assertEquals(200, response.getStatusCode()); assertEquals("janet.weaver@reqres.in",response.jsonPath().getString("data.email")); assertTrue(response.jsonPath().getString("data.first_name").contains("Janet")); } } |
Output:
1 2 3 4 5 6 7 8 | [RemoteTestNG] detected TestNG version 7.0.1 Response :{"data":{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}} PASSED: getExample =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== |
GET REQUEST WITH QUERY PARAMETERS
Query Parameter is used to filter or sort the resources. In the example below, we are going to use queryParam() with the get() call.
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 | /** * GET Request with Query Parameters * @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_GET_with_Query_Parameters_Example { @Test public void getQueryParamExample() { RestAssured.baseURI = "https://reqres.in/api"; Response response = null; try { response = RestAssured.given() .when().queryParam("page", 2) .get("/users"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); assertEquals(200, response.getStatusCode()); assertEquals("Lindsay",response.jsonPath().getString("data[1].first_name")); } } |
Output:
1 2 3 4 5 6 7 8 | [RemoteTestNG] detected TestNG version 7.0.1 Response :{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"michael.lawson@reqres.in","first_name":"Michael","last_name":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},{"id":8,"email":"lindsay.ferguson@reqres.in","first_name":"Lindsay","last_name":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},{"id":9,"email":"tobias.funke@reqres.in","first_name":"Tobias","last_name":"Funke","avatar":"https://reqres.in/img/faces/9-image.jpg"},{"id":10,"email":"byron.fields@reqres.in","first_name":"Byron","last_name":"Fields","avatar":"https://reqres.in/img/faces/10-image.jpg"},{"id":11,"email":"george.edwards@reqres.in","first_name":"George","last_name":"Edwards","avatar":"https://reqres.in/img/faces/11-image.jpg"},{"id":12,"email":"rachel.howell@reqres.in","first_name":"Rachel","last_name":"Howell","avatar":"https://reqres.in/img/faces/12-image.jpg"}],"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}} PASSED: getQueryParamExample =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== |
GET REQUEST WITH PATH PARAMETERS
Path Parameter is used to identify a specific resource or resources. In the example below, we are going to use pathParam() with the get() call.
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 | /** * GET Request with Path Parameters * @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_GET_with_Path_Parameters_Example { @Test public void getPathParamExample() { String id = "3"; RestAssured.baseURI = "https://reqres.in/api/users"; Response response = null; try { response = RestAssured.given() .pathParam("id", id) .when() .get("/{id}"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); assertEquals(200, response.getStatusCode()); assertEquals("Wong",response.jsonPath().getString("data.last_name")); } } |
Output:
1 2 3 4 5 6 7 8 | [RemoteTestNG] detected TestNG version 7.0.1 Response :{"data":{"id":3,"email":"emma.wong@reqres.in","first_name":"Emma","last_name":"Wong","avatar":"https://reqres.in/img/faces/3-image.jpg"},"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}} PASSED: getPathParamExample =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== |
rest assured examples rest assured examples
POST REQUEST
The HTTP POST request is used to send data to a server to create the resource. In the example below, we are going to use post() method of Rest Assured Java Library and sending the data while making the call.
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 39 40 41 42 43 44 45 46 | /** * POST Request * @author Deepak Verma */ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; public class RestAssured_POST_Example { @Test public void postExample() { RestAssured.baseURI = "https://reqres.in/api"; String requestBody = "{\n" + " \"name\": \"Sam\",\n" + " \"job\": \"Project Leader\"\n" + "}"; Response response = null; try { response = RestAssured.given() .contentType(ContentType.JSON) .body(requestBody) .post("/users"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); assertEquals(201, response.getStatusCode()); assertTrue(response.jsonPath().getString("name").contains("Sam")); assertFalse(response.jsonPath().getString("job").equals("Leader")); } } |
Output:
1 2 3 4 5 6 7 8 | [RemoteTestNG] detected TestNG version 7.0.1 Response :{"name":"Sam","job":"Project Leader","id":"26","createdAt":"2022-04-16T07:55:06.292Z"} PASSED: postExample =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== |
rest assured examples rest assured examples
PUT REQUEST
PUT request is used to update the resource on the server. In the example below, we are going to use put() method and supply the data while making the call.
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 39 40 41 42 43 44 | /** * PUT Request * @author Deepak Verma */ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; public class RestAssured_PUT_Example { @Test public void putExample() { RestAssured.baseURI = "https://reqres.in/api"; String requestBody = "{\n" + " \"name\": \"Adam\",\n" + " \"job\": \"Software Engineer\"\n" + "}"; Response response = null; try { response = RestAssured.given() .contentType(ContentType.JSON) .body(requestBody) .put("/users/2"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); assertEquals(200, response.getStatusCode()); assertTrue(response.jsonPath().getString("job").contains("Software")); } } |
Output:
1 2 3 4 5 6 7 8 | [RemoteTestNG] detected TestNG version 7.0.1 Response :{"name":"Adam","job":"Software Engineer","updatedAt":"2022-04-16T07:56:32.506Z"} PASSED: putExample =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== |
rest assured examples rest assured examples
PATCH REQUEST
PATCH request is also used to update the resource just like PUT request but only required to send the only data which is required to be updated. In the example below, we are going to use patch() method and supply the data while making the call.
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 39 40 41 42 43 | /** * PATCH Request * @author Deepak Verma */ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; public class RestAssured_PATCH_Example { @Test public void patchExample() { RestAssured.baseURI = "https://reqres.in/api"; String requestBody = "{\n" + " \"name\": \"Isha\"\n" + "}"; Response response = null; try { response = RestAssured.given() .contentType(ContentType.JSON) .body(requestBody) .patch("/users/2"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); assertEquals(200, response.getStatusCode()); assertTrue(response.jsonPath().getString("name").contains("Isha")); } } |
Output:
1 2 3 4 5 6 7 8 | [RemoteTestNG] detected TestNG version 7.0.1 Response :{"name":"Isha","updatedAt":"2022-04-16T07:59:01.785Z"} PASSED: patchExample =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== |
rest assured examples rest assured examples
DELETE REQUEST
DELETE request is used to delete the resource on the server. In the example below, we are going to use delete() method.
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 | /** * DELETE Request * @author Deepak Verma */ import static org.testng.Assert.assertEquals; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; public class RestAssured_DELETE_Example { @Test public void deleteExample() { RestAssured.baseURI = "https://reqres.in/api"; Response response = null; try { response = RestAssured.given() .contentType(ContentType.JSON) .delete("/users/2"); } catch (Exception e) { e.printStackTrace(); } assertEquals(204, response.getStatusCode()); } } |
Output:
1 2 3 4 5 6 7 | [RemoteTestNG] detected TestNG version 7.0.1 PASSED: deleteExample =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== |
Elaborative and yet easy representation of REST API testing using a REST Assured examples...!!! Share on X
Do you like this Post? – then check out my other helpful posts:
- Serialization and Deserialization in Rest Assured
- Basic Authentication using Rest Assured
- JSON Schema Validation with Rest Assured
- Configure Rest Assured in Eclipse
- Post XML request using Rest Assured
- Upload File using Rest Assured
- Parse JSON Response in Rest Assured
- Test REST API using Karate
- Test GET Request using Karate
- Test POST Request using Karate
- Test PUT Request using Karate
- Test DELETE Request using Karate
Other Useful References