In this example, we are going to see “How to send a DELETE request using Karate to a Rest API endpoint”.
Simple example of DELETE Request using Karate...!!! Share on X
In this tutorial, we are going to cover below topics:
- What is HTTP DELETE Request?
- How to send DELETE request using Karate?
- Response Validation
Let’s begin:
1. What is HTTP DELETE Request?
DELETE method is quite easy and straightforward to understand as it’s 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 resource is already gone.
- But, because of 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 Karate?
Let’s take an example of one of the API DELETE endpoint available at the above-mentioned website which is ‘/delete/{id}’. The full-service URL with endpoint is ‘http://dummy.restapiexample.com/api/v1/delete/{id}’.
At the above resource URL, we are going to make a delete call to remove an existing employee who is having ‘id’ as ‘11400’.
NOTE: This ‘id’ belongs to the employee which is generated during the POST call to create the employee. So, this id : 11400 generated for me when I had executed the create employee 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 place where I am using ‘11400’.
Code to send the DELETE request to the above mentioned Service Endpoint (deleteRequestTest.feature):
1 2 3 4 5 6 7 8 | Feature: Delete Employee Scenario: Verify that the existing employee is successfully getting deleted Given url 'http://dummy.restapiexample.com/api/v1/delete/11400' And method delete Then status 200 And print 'Response is: ', response |
Let’s try to understand the code:
1. Specifying the Feature & Scenario (typical cucumber format)
1 2 3 | Feature: Delete Employee Scenario: Verify that the existing employee is successfully getting deleted |
2. Setting up Endpoint URI in ‘Given’
1 | Given url 'http://dummy.restapiexample.com/api/v1/delete/11400' |
Endpoint URI is the address to the Resource. And, by this particular line of code, we are specifying to Karate to use “dummy.restapiexample.com/api/v1” as the root URL of the service.
3. Specifying the type of request (DELETE) in ‘When’ condition
1 | When method delete |
4. Checking up Status Code & other Response validations in ‘Then’
1 2 | Then status 200 And print 'Response is: ', response |
3. Response Validation
a. status 200 : It will check the status code coming back from the service is 200
b. print ‘Response is: ‘, response : This line of code will print the response from the service in the console.
Eclipse Console Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ... {"success":{"text":"successfully! deleted Records"}} 23:31:27.540 [main] DEBUG com.intuit.karate - response time in milliseconds: 1788 23:31:27.657 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $ 23:31:27.669 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $ 23:31:27.669 [main] INFO com.intuit.karate - [print] Response is: { "success": { "text": "successfully! deleted Records" } } 1 Scenarios (1 passed) ... |
That’s it, it’s that simple to make a DELETE Request using Karate API: ?
Simple example of DELETE Request using Karate...!!! Share on XIf you like this post , please check out my other useful blog posts:
- How to send a POST Request using Rest Assured
- How to make a GET Request using Apache HttpClient in Java
Other Useful References: