Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful Http DELETE method in .NET

Tags:

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?

like image 291
VIBA Avatar asked May 23 '10 19:05

VIBA


People also ask

How do I write a delete method in REST API?

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.

How do you implement a Delete method?

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.

What is Delete method in API?

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.


1 Answers

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).

like image 132
Anero Avatar answered Sep 25 '22 11:09

Anero