RESTful Http DELETE method in .NET

VIBA picture VIBA · May 23, 2010 · Viewed 43k times · Source

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. I need help 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?

Answer

Anero picture Anero · May 23, 2010

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