Language: EN

consumir-un-api-rest-en-c-facilmente-con-restsharp

Consuming a Rest API in C# easily with Restsharp

RestSharp is a popular .Net library 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 particularly complex, the use of RestSharp substantially simplifies the process.

In addition, RestSharp provides additional functions 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 are going to perform the same example as in the previous post, but using RestSharp. The code to consume our example NodeJs Rest API that we saw in this 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 functions such as authentication or serialization.

Of course, if we run the code, we will see in the command prompt the same results 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 properly.

esp8266-client-api-rest-nodejs

In summary, RestSharp is a very popular and interesting library that greatly simplifies our work as a client with Rest API 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 abundant documentation about the library on the project’s website https://restsharp.dev/

It is available as a Nuget package so we can easily add it to our project. It is available for .Net Framework and .Net Standard.