Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP DELETE request

I'm attempting to do an HTTP DELETE in C# from my code behind and am unable to do this. After looking at the members of the WebRequestMethods.Http type, i'm not even sure that this is possible.

Here is my code:

try
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/NameFiles/00000.txt");
    request.Method = "DELETE";
    request.ContentType = "application/x-www-form-urlencoded";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    if (response.StatusCode == HttpStatusCode.OK)
    {
        // some code
    }
}
catch (Exception ex)
{
    ex.ToString();
}

Running this from my development environment I get: "The remote server returned an error: (401) Unauthorized."

I have received a different result on a server that I assume has something to do with settings in IIS: "The remote server returned an error: (501) Not Implemented."

Also, as I mentioned in a comment to an answer below, I am able to send DELETE requests from a classic asp page using vbscript on the same server to the same location as the request from my aspx page using c#. Why would these be different?

like image 788
Jeremy Cron Avatar asked Aug 11 '10 17:08

Jeremy Cron


People also ask

Should delete return 200 or 204?

A 204 ( No Content ) status code if the action has been enacted and no further information is to be supplied. A 200 ( OK ) status code if the action has been enacted and the response message includes a representation describing the status.

Does http delete have request body?

The only means to accept the content for DELETE method in IS is by passing the Query parameter. Abdul Basith Shaik: This is an know scenario and Integration Server doesn't accept request body for HTTP DELETE Method. The only means to accept the content for DELETE method in IS is by passing the Query parameter.

Which method perform http delete?

The DELETE method deletes the specified resource. The CONNECT method establishes a tunnel to the server identified by the target resource. The OPTIONS method describes the communication options for the target resource. The TRACE method performs a message loop-back test along the path to the target resource.

Can you delete with GET request?

If you use GET for a DELETE you are clearly misusing the method according to its standard definition. This will not respond to a get request, (so for example, accidental deletion instead of retrieval when making a call to the API is more protected - the developer has to explicitly perform a DELETE request to the API).


1 Answers

  1. You should remove Content-Type header which is useless here.
  2. Check if you are using IIS and if files/ folder that you are trying to delete has rights to delete. These are the user that I can think of: The IUSR or IUSR_MachineName account. The IIS_IUSRS or IIS_WPG group.
  3. For 501 error return - PUT, DELETE , OPTIONS are not enabled by default. Hence, you need to enable at web server level.

You should make sure that the following configurations are there in the config file. You can also see some other post related with delete. (modify below settings that suites your environment).

 <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
like image 72
codebased Avatar answered Sep 19 '22 07:09

codebased