RestSharp is a popular library for .Net that allows us to act as clients to consume a REST API.
In a previous post, we saw how to consume a REST API from C# without using third-party libraries. Although the code is not especially complex, using RestSharp substantially simplifies the process.
Furthermore, RestSharp provides additional features such as native serialization to XML or JSON, identification of the request or response type, or authentication (Basic, OAuth, NTLM…).
To illustrate its use, we will perform the same example as in the previous post, but using RestSharp. The code to consume our example REST API in NodeJs that we saw in that post would be as follows.
static void Main(string[] args)
{
GetItem(10);
GetItems();
GetItems("ABC");
PostItem("NewItem");
PutItem(4, "ReplaceItem");
DeleteItem(5);
Console.ReadLine();
}
private static void GetItem(int id)
{
var client = new RestClient("http://localhost:8080");
var request = new RestRequest($"/item/{id}", Method.GET);
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
private static void GetItems()
{
var client = new RestClient("http://localhost:8080");
var request = new RestRequest("items", Method.GET);
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
private static void GetItems(string filter)
{
var client = new RestClient("http://localhost:8080");
var request = new RestRequest("items", Method.GET);
request.AddParameter("filter", filter);
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
private static void PostItem(string data)
{
var client = new RestClient("http://localhost:8080");
var request = new RestRequest("items", Method.POST);
request.AddParameter("data", data);
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
private static void PutItem(int id, string data)
{
var client = new RestClient("http://localhost:8080");
var request = new RestRequest("items", Method.PUT);
request.AddParameter("id", id);
request.AddParameter("data", data);
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
private static void DeleteItem(int id)
{
var client = new RestClient("http://localhost:8080");
var request = new RestRequest($"items/{id}", Method.DELETE);
var response = client.Execute(request);
Console.WriteLine(response.Content);
}
As we can see, the code developed with RestSharp is much more concise, simple, and maintainable. The difference would be even greater if we were using advanced features like authentication or serialization.
Of course, if we run the code, we will see the same results in the command console as in the previous post, demonstrating that everything works correctly, as well as in the NodeJs console, where we see that the requests are received and responded to appropriately.

In summary, RestSharp is a very popular and interesting library that greatly simplifies our work as a client with REST APIs from .Net applications.
RestSharp is Open Source, and all its code is available at the following link: https://github.com/restsharp/RestSharp. It is advisable to consult the extensive documentation about the library on the project’s website: https://restsharp.dev/
It is available as a NuGet package, so we can comfortably add it to our project. It is available for .Net Framework and .Net Standard.

