As a part of End to End REST Assured Tutorial , in this post We will write first DELETE request in REST assured.
I will strongly recommended you to learn basics of API testing from here.
To learn what is DELETE request and how can you hit a DELETE request in Postman tool, must refer this post.
We will delete a booking details using “DeleteBooking” API provided by Restful Booker. Once deleted, we will verify it using GetBooking API.
Refer Delete Booking API documentation to understand how authentication and body need to be provided to request.
Below points summarize the above documentation :-
- We need to pass booking id which you want to delete in URI. E.g. https://restful-booker.herokuapp.com/booking/1 where “1” is booking id.
- Authentication token need to pass as cookie. Cookie name is “token” and value is generated auth token. ‘Cookie: token=<generatedToken>’.
- You don’t required to pass body to DELETE request.
Let’s see existing details of a Booking ID 1 using Postman:-
Let’s generate token which I have already covered in Previous post. Perform all above steps in Postman tool as of now. We will see in upcoming posts to chain different requests in REST Assured.
Generate Token:-
Let’s write DELETE request in REST Assured:-
Non-BDD Style:-
package RestfulBooker.deleteExamples; import io.restassured.RestAssured; import io.restassured.response.Response; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; public class NonBDDDeleteRequest { public static void main(String[] args) { // Create a request specification RequestSpecification request = RestAssured.given(); // Setting a cookie for authentication as per API documentation request.cookie("token", "f4e70e7b9bbcd05"); // Adding URI request.baseUri("https://restful-booker.herokuapp.com/booking/1"); // Calling PUT method on URI. After hitting we get Response Response response = request.delete(); // Printing Response as string System.out.println(response.asString()); // Get Validatable response to perform validation ValidatableResponse validatableResponse = response.then(); // Validate status code as 201 as per API documentation validatableResponse.statusCode(201); // Validate if booking is actually deleted. RequestSpecification getRequestSpec = RestAssured.given(); // Adding URI getRequestSpec.baseUri("https://restful-booker.herokuapp.com/booking/1"); // Calling GET request Response res = getRequestSpec.get(); // Get Validatable response to perform validation ValidatableResponse valRes = res.then(); // It will check if status code is 404 as booking id should not be found valRes.statusCode(404); } }
BDD Style:-
package RestfulBooker.deleteExamples; import io.restassured.RestAssured; public class BDDStyleDeleteRequest { public static void main(String[] args) { // Delete Booking //GIVEN RestAssured .given() .baseUri("https://restful-booker.herokuapp.com/booking/1") .cookie("token", "f7dddb1093eab19") // WHEN .when() .delete() // THEN .then() .assertThat() .statusCode(201); // Verifying booking is deleted // Given RestAssured .given() .baseUri("https://restful-booker.herokuapp.com/booking/1") // When .when() .get() // Then .then() .statusCode(404); } }
Note:- You need to generate access token every time you hit the request otherwise you will get error code as 403.
You can clone/download example repo here.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.
why would a delete request return status code 201 as Created when we are deleting a resource
In fact there is no rules that a delete call can not return 201. It depends upon developer.