Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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
Get the data for the user using the Get User Data API.
Delete a data corresponding and thus validate the response code using the DELETE request.
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);
}
}
You can also try this code with Online Java Compiler
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.
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: