In this tutorial, we are going to see “How to send a DELETE request using Rest-Assured to a Rest API endpoint”.
Simple representation of DELETE Request using RESTAssured...!!! Click To Tweet
In this tutorial, we are going to cover the below topics:
- What is the HTTP DELETE Request?
- How to send DELETE request using Rest Assured?
- How to validate the Response?
Let’s begin:
1. What is the HTTP DELETE Request?
The DELETE method is quite easy and straightforward to understand as its name suggest, it is used to ‘Delete’ any resource specified by a URI.
Some key points of DELETE requests:
- DELETE is idempotent means if you try to make a request multiple times, it would result in the same output as it would have no effect. Therefore, sending a ‘DELETE’ request again and again on the same resource would end up in the same result as the resource is already gone.
- But, because of the above point, calling ‘Delete’ second time on the same resource would result in 404 (Not Found) status code since it was already gone. This, actually makes DELETE no longer idempotent.
- ‘200’ (OK) status codes will be returned if the resource gets successfully deleted and the response message representation stating the status while ‘204’ (No Content) could be returned if the action has been enacted but the response body is empty.
2. How to send DELETE 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 DELETE endpoints available at the above-mentioned website which is ‘/users/{id}’. The full-service URL with the endpoint is ‘http://reqres.in/api/users/{id}’.
At the above resource URL, we are going to make a delete call to remove an existing user who is having ‘id’ as ‘11400’.
NOTE: This ‘id’ belongs to the user which is generated during the POST call to create the user. So, this id: 11400 generated for me when I had executed the create user using the POST call. In order to get yours, first, execute the POST Request call available at this link and grab the ‘id’ from the response and use it in your DELETE Request Test in the place where I am using ‘11400’.
Here is the code to send the DELETE request to the above-mentioned REST API 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 | /** * 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_DeleteRequest { @Test public void deleteUser() { RestAssured.baseURI = "https://reqres.in/api"; Response response = null; try { response = RestAssured.given() .contentType(ContentType.JSON) .delete("/users/11400"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Status Code: " + response.getStatusCode()); } } |
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 DELETE request to that resource
1 2 3 4 | Response response = null; response = RestAssured.given() .contentType(ContentType.JSON) .delete("/delete/11400"); |
Here, using this code, we are looking to make a DELETE request to an exact resource which is “/users/11400” in this case. Hence, the complete Service Endpoint would be “http://reqres.in/api/users/11400” to which we are sending a DELETE request. The output of the DELETE call will be stored in the REST Assured ‘Response’ object.
3. Response validation
1 | System.out.println("Status Code: " + response.getStatusCode()); |
response.getStatusCode() : This line of code would extract the status code from the response.
3. How to validate the Response?
I’ve already explained the validation above. Now, Let’s just execute the above Test class (DELETERequest) in eclipse and verify the output:
a. Check for the Response. For this specific API endpoint, Response is blank. (you can try to print that in the console).
b. Check for the Status Code i.e ‘204’ as intended.
Eclipse Console Output:
1 2 3 4 5 6 7 8 | [RemoteTestNG] detected TestNG version 7.0.1 Status Code: 204 PASSED: deleteUser =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== |
That’s it, it’s that simple to make a DELETE Request using REST Assured API.
Simple representation of DELETE Request using RESTAssured...!!! Click To TweetIf you like this post, please check out my other useful blog posts on Rest Assured:
Other Useful References: