REST calls from back-end using RestSharp

There are times you want to make a call to an external REST API (not SOAP web services) from your back-end C# code.

Well, this can be achieved by utilizing HttpClient:

HttpClient client = new HttpClient();

This is fine. Only you have to take care of the rest of code complexity – especially in the case of an async call. RestSharp library can help you hide all those complexities and focus on your code logic. Using this library is very easy. First, just install it from NuGet package manager or do it via command:

PM> Install-Package RestSharp Then in you (service layer) code instantiate the REST client as:

var client = new RestClient(ApiUrl);
var request = new RestRequest("/api/ControllerName/{id}/{type}", Method.GET);

ApiUrl is the service address (e.g. http://example.com)

{id} and {type} are parameter place holder which we will define later to keep a cleaner code.

request.AddHeader("Content-Type", "application/json");
request.AddHeader("Cache-Control", "no-cache");
request.RequestFormat = DataFormat.Json;
request.AddUrlSegment("id", id.ToString());
request.AddUrlSegment("type", type);

The above code is just a sample configuration for our GET request.

In case we want to post data, we can write something like that:

...
var request = new RestRequest("/api/ControllerName", Method.POST);
...
var body = new
{
    Id = id,
    Type= type
};
request.AddBody(body);

And then we execute the request:

var response = client.Execute(request);

if (response.StatusCode == HttpStatusCode.OK)
{
    // some logic
}

The async call is just very similar:

client.ExecuteTaskAsync(request);

You can still Await the response you get from this async call, or you can call and forget. In the above code, your thread will not be blocked until it receives the response.


© 2019. All rights reserved.