The DELETE method requests that the origin server delete the resource recognized by the Request -URI.
Till now, we have learned about how to make a PUT Request using Rest-Assured. In addition to that, we also learned to read different components of HTTP Response (Headers, Body, and Status). In case you have not read the previous tutorials, please have a look at the list of tutorials here:
In this article, we will understand the following things: -
- First, Basics of Delete Verb/Method/Request
- Second, DELETE Request using Rest Assured
- Third, Validating the Response
What is a DELETE Request?
DELETE is pretty easy to understand. Its primary purpose is to DELETE a resource identified by a URI.
The official HTTP RFC specifies:
- The DELETE method requests the origin server to delete the resource identified by the Request-URI.
- The server implementation determines if the actual content will be deleted or not. Additionally, it will check whether it can restore or not.
- The DELETE method requests that the origin server deletes the association between the target resource and its current functionality.
- Moreover, this method is like the rm command in UNIX. It signifies a deletion operation on the URI mapping of the origin server. As per expectations, the previously associated information with the URI does not delete.
- Finally, in addition to the above, responses to this method are not cache-able.
Appropriate status code for DELETE method
- 202 Accepted- if the action will likely succeed but has not yet enacted.
- 204 No Content- If successful and if no further information requires to supply.
- 200 OK - If the action executes and the response message includes a representation describing the status.
REST API Example:
Endpoint | http://dummy.restapiexample.com/delete/15410 |
HTTP Method Type | DELETE |
Body | We don’t need to pass body for DELETE request in this example |
Success Response | Success Status code: 200 OK { "success": { "text":"successfully! deleted Records" } } |
Note: In the example above, the creation of employee ID 15410 happens beforehand as a resource on the server. In addition, we delete the associated employee information in the DELETE request.
How to make a DELETE Request using Rest Assured?
Follow the steps one by one to understand the code written.
1st Step: Create a variable empid and specify the value to be deleted.
int empid = 15410;
2nd Step: Specify a Request pointing to the Service Endpoint.
RestAssured.baseURI = "https://dummy.restapiexample.com/api/v1";
RequestSpecification request = RestAssured.given();
3rd Step: Send the Delete Request as described below.
// Add a header stating the Request body is a JSON
request.header("Content-Type", "application/json");
/* Delete the request and check the response
* The actual request being passed equalizes to http://dummy.restapiexample.com/api/v1/delete/15410
* Here, we capture the response for DELETE request by passing the associated empID in the baseURI */
Response response = request.delete("/delete/"+ empid);
4th Step: Validate the PUT Request response received.
int statusCode = response.getStatusCode();
System.out.println(response.asString());
Assert.assertEquals(statusCode, 200);
String jsonString =response.asString();
Assert.assertEquals(jsonString.contains("successfully! deleted Records"), true);
We validate the status code obtained to be equivalent to 200 OK. Additionally, we capture the response and convert it to String. As a result, this response is asserted as true, which contains the associated message: “successfully! deleted Records”.
The complete code looks like below:
import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
@Test
public void deleteEmpRecord() {
int empid = 15410;
RestAssured.baseURI = "https://dummy.restapiexample.com/api/v1";
RequestSpecification request = RestAssured.given();
// Add a header stating the Request body is a JSON
request.header("Content-Type", "application/json");
// Delete the request and check the response
Response response = request.delete("/delete/"+ empid);
int statusCode = response.getStatusCode();
System.out.println(response.asString());
Assert.assertEquals(statusCode, 200);
String jsonString =response.asString();
Assert.assertEquals(jsonString.contains("successfully! deleted Records"), true);
}
Summary
To conclude, in this article, we have learned about the basics of DELETE Request and how to use it in Rest Assured. Moreover, we asserted the response by status code. In addition to that, the response message obtained contains an equivalent user-friendly message conveying that the successful record deletion is correct as well as stated.
Please try to implement these HTTP methods that we have covered in the Rest API testing using Rest-Assured tutorial. And consequently, provide your valuable feedback.