C# + RestSharp - HTTP DELETE Request Examples in .NET
Tutorial built with .NET 7.0 and RestSharp 108.0.3
Below is a quick set of examples to show how to send HTTP DELETE requests from .NET to an API using the RestSharp
HTTP client which is available on NuGet.
Other RestSharp HTTP examples: GET, POST, PUT.
Tutorial contents
- Installing RestSharp
- Simple DELETE request
- DELETE request with async/await
- DELETE request with headers set
- DELETE request with error handling
Installing RestSharp from NuGet
.NET CLI: dotnet add package RestSharp
Visual Studio Package Manager Console: Install-Package RestSharp
Simple DELETE request using RestSharp
This sends an HTTP DELETE request to the Test JSON API which is a fake online REST API that includes a product details route (/products/{id}
), the route responds to DELETE
requests with the JSON object { "message": "Product deleted" }
.
The response type is deserialized to <JsonNode>
so it can handle any dynamic properties returned in the response.
using RestSharp;
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
public class Program
{
public static void Main()
{
// send DELETE request with RestSharp
var client = new RestClient("https://testapi.jasonwatmore.com");
var request = new RestRequest("products/1", Method.Delete);
var response = client.Execute(request);
// deserialize json string response to JsonNode object
var data = JsonSerializer.Deserialize<JsonNode>(response.Content!)!;
// output result
Console.WriteLine($"""
----------------
json properties
----------------
message: {data["message"]}
----------------
raw json data
----------------
{data}
----------------
Documentation at https://jasonwatmore.com/c-restsharp-http-delete-request-examples-in-net
""");
}
}
Example C# app on .NET Fiddle at https://dotnetfiddle.net/wrRJUx
DELETE request using RestSharp with async/await
This sends the same DELETE request from .NET using RestSharp, but this version uses an async
method and the await
C# operator to wait for the asynchronous HTTP request to complete without blocking the thread that called the async method.
using RestSharp;
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
public class Program
{
public static async Task Main()
{
// send DELETE request with RestSharp
var client = new RestClient("https://testapi.jasonwatmore.com");
var request = new RestRequest("products/1", Method.Delete);
var response = await client.ExecuteAsync(request);
// deserialize json string response to JsonNode object
var data = JsonSerializer.Deserialize<JsonNode>(response.Content!)!;
// output result
Console.WriteLine($"""
----------------
json properties
----------------
message: {data["message"]}
----------------
raw json data
----------------
{data}
----------------
Documentation at https://jasonwatmore.com/c-restsharp-http-delete-request-examples-in-net
""");
}
}
Example C# app on .NET Fiddle at https://dotnetfiddle.net/oo6lV6
DELETE request using RestSharp with headers set
This sends the same request again with a couple of headers set, the HTTP Authorization
header and a custom header My-Custom-Header
.
To update existing headers call the AddOrUpdateHeader()
method, e.g. request.AddOrUpdateHeader("Authorization", "Bearer my-token");
.
using RestSharp;
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
public class Program
{
public static void Main()
{
// send DELETE request with RestSharp
var client = new RestClient("https://testapi.jasonwatmore.com");
var request = new RestRequest("products/1", Method.Delete);
request.AddHeader("Authorization", "Bearer my-token");
request.AddHeader("My-Custom-Header", "foobar");
var response = client.Execute(request);
// deserialize json string response to JsonNode object
var data = JsonSerializer.Deserialize<JsonNode>(response.Content!)!;
// output result
Console.WriteLine($"""
----------------
json properties
----------------
message: {data["message"]}
----------------
raw json data
----------------
{data}
----------------
Documentation at https://jasonwatmore.com/c-restsharp-http-delete-request-examples-in-net
""");
}
}
Example C# app on .NET Fiddle at https://dotnetfiddle.net/6RyMbL
DELETE request using RestSharp with error handling
This sends a DELETE request from .NET using RestSharp to an invalid url on the api then logs the error message to the console.
RestSharp Execute()
vs Delete()
There are a couple of methods to perform a Delete request with RestSharp - Execute()
and Delete()
(plus their async and generic versions). The difference between the two is how they deal with error handling when a request fails.
In case of a failed HTTP request the Execute()
method returns a RestReponse
that contains the ErrorException
, whereas the Delete()
method throws an exception when a request fails unless it's a 404, so would require a try-catch
block to handle errors.
using RestSharp;
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
public class Program
{
public static void Main()
{
// send DELETE request with RestSharp
var client = new RestClient("https://testapi.jasonwatmore.com");
var request = new RestRequest("invalid-url", Method.Delete);
var response = client.Execute(request);
// handle error
if (!response.IsSuccessful)
{
Console.WriteLine($"ERROR: {response.ErrorException?.Message}");
return;
}
// deserialize json string response to JsonNode object
var data = JsonSerializer.Deserialize<JsonNode>(response.Content!)!;
// output result
Console.WriteLine($"""
----------------
json properties
----------------
message: {data["message"]}
----------------
raw json data
----------------
{data}
----------------
Documentation at https://jasonwatmore.com/c-restsharp-http-delete-request-examples-in-net
""");
}
}
Example C# app on .NET Fiddle at https://dotnetfiddle.net/YF26An
Need Some .NET Help?
Search fiverr for freelance .NET developers.
Follow me for updates
When I'm not coding...
Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!