Search code examples
c#resthttphttp-delete

RESTful HTTP DELETE method in .NET


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?


Solution

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