Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Features of DELETE Request
3.
Different Response Codes
4.
Steps to Write Delete Request
5.
Code
5.1.
Output
6.
Code Explanation
7.
Frequently Asked Questions
7.1.
How do you delete an HTTP request?
7.2.
When you submit a delete request, what happens?
7.3.
Should HTTPS DELETE return 200 or 204?
7.4.
Can Delete have query parameters?
7.5.
Can we delete a resource using the POST method?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Let’s Write First DELETE Request in REST Assured

Author Yashi Agarwal
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

An HTTP delete request is carried out using the method of HTTP delete (written as Delete), which helps delete the data from the server identified by the URI we send. Also, note that a delete method always intends to change the server's state. However, even a 200 code does not guarantee this.

DELETE request in Rest Assured

Features of DELETE Request

  • The Delete method has to request the server to delete the resource identified by the request URI(Uniform Resource Identifier).
  • The resource deletion then depends on the server and is deleted only if it is prescribed for the deletion process. Additionally, the restoration implementation of the particular resource is also considerable.
  • There is a deletion request for the association between the resource and the corresponding current functionality.
  • It is also similar to the rm UNIX command, wherein that resource's current association and connection are deleted instead of the previous ones(if any).
  • The delete request method is now placed under the idempotent category of the W3C documentation. Thus, once a resource is requested for the deletion process, a request on the same resource would give the same result as the help data that has already been deleted.
  • The delete method response is considered non-cacheable. The user cannot thus cache the server response for later use and purpose. Caching a delete request creates inconsistencies.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Different Response Codes

  • 202(Accepted) - The server has accepted the request but does not fully enact it.
  • 204(No Content) - The status code of 204 on the HTTP delete request method thus denotes that the successful enactment of the delete request takes place in the absence of the response.
  • 200(OK) - TThe response message indicates that the task was successful.
  • 404(Not Found) - When the server cannot find the particular resource. The reason could either not exist or have previously been deleted.

Steps to Write Delete Request

  1. Get the data for the user using the Get User Data API.
  2. Delete a data corresponding and thus validate the response code using the DELETE request.
  3. Verify that the API execution from step 1 updates the given data and does not display the deleted information.

Code

import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import junit.framework.Assert;
public class Delete_Test {
    @Test
    void Test_Delete() {
        String baseUrl = "https://reqres.in/";
        RestAssured.baseURI = baseUrl;
        //Calling the Delete API with a request body
        RequestSpecification httpRequest = (RequestSpecification) RestAssured.given()
            .header("Content-Type", "application/json");
        Response res = delete("https://reqres.in/api/users/2");
        //Fetching the response code from the request and validating the same
        System.out.println("The response code is - " + res.getStatusCode());
        Assert.assertEquals(res.getStatusCode(), 204);
    }
}

Output

Output

We have successfully passed the test case.

Code Explanation

We now first set up the request with the base URI. Next, we add the header(s), including the Authorization token using the RequestSpecification interface. Next, store the Response object using the Request Specification object. Here we have to pass the JSON body of our request and the endpoint URL for the Delete method. The response thus gets stored in a variable. Lastly, we must print the response status code and use the TEST NG ASSERT to validate the same process. Since, In this case, the expected response code is somewhat near 204, our test would have it as the desired parameter. And there you go! You can successfully delete a record using the method of Delete API.

Frequently Asked Questions

How do you delete an HTTP request?

There is a simple way to Delete and put a request, and you can do it by adding a " _method " parameter to your post request and writing " PUT " or " DELETE " for its value!

When you submit a delete request, what happens?

The HTTP DELETE method is required and used in deleting a resource from the server. Unlike GET and HEAD requests, the DELETE requests may change the server state. Sending a message body on a DELETE request might cause some servers to reject the request. But you can still send the data to the server using URL parameters.

Should HTTPS DELETE return 200 or 204?

A 204 ( No Content ) status code if the particular action has been enacted and no information is to be supplied further. A 200 ( OK ) status code if the specific action has been completely enacted; thus, the response message includes a representation that helps describe the status.

Can Delete have query parameters?

There's nothing wrong with using DELETE on a collection and filtering by query parameters.

Can we delete a resource using the POST method?

You may use the POST method to create, update and delete resources, but this is considered a poor design. The different HTTP verbs standardize modern ways of creating REST APIs.

 

Also See - Difference between argument and parameter

Conclusion

So basically, in this article, we have learned how to write a DELETE request in Rest Assured. Three main steps are needed to be followed. I have mentioned an explanation for each point. To learn more about Rest API, check the link below:

Refer to our guided paths on Coding Ninjas 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. Take a look at the interview experiences and interview bundle for placement preparations.

Keep Coding!

Live masterclass