I am new to web services. I am dealing with testing APIs in my project. In the previous version the company used GET and POST methods but not PUT and DELETE methods. For the HTTP DELETE method, I have browsed various websites where I found the example code snippets for GET and POST methods, but not for DELETE and PUT methods (why?).
Can anyone give me an example code snippet (C#) for RESTful HTTP DELETE method and explain how to call the DELETE request?
In REST API DELETE is a method level annotation, this annotation indicates that the following method will respond to the HTTP DELETE request only. It is used to delete a resource identified by requested URI. DELETE operation is idempotent which means. If you DELETE a resource then it is removed, gone forever.
As you can see in the above figure, HTTP DELETE request url http://localhost:64189/api/student?id=1 includes query string id. This id query string will be passed as an id parameter in Delete() method. After successful execution the response status is 200 OK.
As the name applies, DELETE APIs delete the resources (identified by the Request-URI). DELETE operations are idempotent. If you DELETE a resource, it's removed from the collection of resources. Some may argue that it makes the DELETE method non-idempotent.
Check out the following code snippet:
string sURL = "<HERE GOES YOUR URL>";
WebRequest request = WebRequest.Create(sURL);
request.Method = "DELETE";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
In the response object you should check the StatusCode property (it should be 200 or 204 if everything goes right, see here for more info).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With