34

I have a simple Restful service being called from a console app so am using WebClient. I am wondering if this call for Delete is correct.

The url looks like localhost/RestService1/Person/1

using (var client = new WebClient()) 
{
    client.UploadString(url, "DELETE", "");
}

I don't like that UploadString does not have an overload without a data parameter. The passing of an empty parameter is not sitting well with me. Is there a better method to use for a DELETE?

I could use WebRequest but I want to just use WebClient to keep it consistent.

Here is the WebRequest block

var request = WebRequest.Create(url);
request.Method = "DELETE";
var response = (HttpWebResponse)request.GetResponse();

Both blocks work fine but what is best? Or is there a better way?

deczaloth
  • 7,094
  • 4
  • 24
  • 59
Jimmy James
  • 381
  • 1
  • 3
  • 6
  • See also: http://stackoverflow.com/questions/2539394/rest-http-delete-and-parameters – Peter Ritchie Sep 11 '12 at 20:57
  • see also: http://stackoverflow.com/questions/512279/restful-delete-strategy – Peter Ritchie Sep 11 '12 at 20:58
  • Other than the relation to DELETE and RESTful in those references, I don't think `WebClient` really gives you the semantics of DELETE. `Webclient` just uses `WebRequest` (`HttpWebRequest`) under the covers, so I think using `HttpWebRequest`) directly is more readable. – Peter Ritchie Sep 11 '12 at 21:04
  • 1
    I don't think the first 2 links refer to my question. My url is localhost/RestService1/Person/1 where /1 is the person Id, so it is restful. I did a little more digging and see that WebClient is mostly a wrapper. thanks – Jimmy James Sep 11 '12 at 22:17

4 Answers4

29

The following works for me:

client.UploadValues(url, "DELETE", new NameValueCollection()); 
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
Simon
  • 291
  • 3
  • 2
3

The WebClient class doesn't really lend well to restful api consumption, I've used 3rd party libraries like RestSharp in the past that are geared more towards this type of web request. I'm pretty sure RestSharp just uses HttpWebRequest under the covers, but it provides a lot of semantics that make consuming and reusing rest resources easier.

RTigger
  • 1,360
  • 10
  • 11
3

Go get the Microsoft.Net.Http client libraries http://nuget.org/packages/Microsoft.Net.Http

HttpClient is a much better client to use for working with an API.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • This package is not supported in Visual Studio 2010, and is only required for projects targeting .NET Framework 4.5 or .NET for Windows Store apps when consuming a library that uses this package. – juFo Apr 12 '13 at 06:54
  • @juFo It is supported in VS2010 and in .net 4.0. I'm not sure why you would think otherwise. – Darrel Miller Apr 12 '13 at 11:08
  • I just quoted the link you provided: http://nuget.org/packages/Microsoft.Net.Http – juFo Apr 12 '13 at 11:54
  • @juFo Aaah. Unfortunately the content of that link changed since I posted it! That's the pre-release "portable library" version of HttpClient. If you go for the stable one you will get a .net 4/VS2010 compatible version. – Darrel Miller Apr 12 '13 at 12:27
  • HttpClient is clumsy in usage, since contains only ASYNC methods (what is not always desirable). Moreover - MS docs says HttpClient is NOT replacement for WebClient. – Vincent May 23 '21 at 13:12
0

Sorry this is my solution in vb.net i sure that anyone can translate to c#

It's very important to drop headers, i had to comment header about Accept and Content-Type and work fine..... of course I did send the token

Dim rest As WebClient = New WebClient()
rest.Headers.Add(HttpRequestHeader.Authorization, "Bearer " & Token)
'rest.Headers.Add(HttpRequestHeader.Accept, "application/json")
'rest.Headers.Add(HttpRequestHeader.ContentType, "application/json")

result = System.Text.Encoding.UTF8.GetString(rest.UploadValues(host_api & uri, "DELETE", New NameValueCollection()))
Chevelle
  • 248
  • 5
  • 13