Working with RestClient


Introduction

The RestClient class is the best option to call restful web APIs. This article demonstrates how to use RestClient class to call Restful Web API.

Getting Started

Now a days the restful APIs or restful services are very popular in software world, to consume rest APIs in c# the RestClient class is one of best option. It is third party library provided by RestSharp and available in NuGet Manager. To consume this library minimum NETFramework 2.0 and later version is required.




You can directly add the reference of this library from NuGet Manager in Microsoft Visual Studio. In NuGet Manager, in Browser just search for RestSharp the latest build will be available in the search list. Currently the latest version 106.6.1 is available, the below image shows the browsing list for RestSharp.

If you are not able to search the library, you can directly use below command in NuGet Package Manager Console. The blow code also shows how to use comand.
 dotnet add package RestSharp --version 106.6.10  

Example:-
The below example provides sample code which calls restful API using RestClient class.
 
       IRestRequest request = new RestRequest(Method.POST);  
       request.AddHeader("content-type", "application/json");  
       request.AddHeader("content-Length","0");  
       request.RequestFormat = DataFormat.Json;  
       request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);  

       RestClient client = new RestClient("url");  

       IRestResponse response = client.Execute(request);
  
       string responsevalue = response.Content;  
Below code snippet helps to upload a file using RestClient class.
 
       IRestRequest request = new RestRequest(Method.POST);  
       request.AddHeader("content-type", "application/json");  
       request.AddHeader("content-Length","0");  
       request.RequestFormat = DataFormat.Json;  
       request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);  
       request.AddFile("file", path);
       RestClient client = new RestClient("url");  

       IRestResponse response = client.Execute(request);
  
       string responsevalue = response.Content;  

Below code snippet download a file using RestClient class.
 
       IRestRequest request = new RestRequest(Method.POST);  
       request.AddHeader("content-type", "application/json");  
       request.AddHeader("content-Length","0");  
       request.RequestFormat = DataFormat.Json;  
       request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);  
       request.AddFile("file", path);
       RestClient client = new RestClient("url");  

       client.DownloadData(request).SaveAs(path);  



Below code snippet async API call.
 
       IRestRequest request = new RestRequest(Method.POST);  
       request.AddHeader("content-type", "application/json");  
       request.AddHeader("content-Length","0");  
       request.RequestFormat = DataFormat.Json;  
       request.AddParameter("Parameter_Name", "Parameter_Value", ParameterType.RequestBody);  
       request.AddFile("file", path);
       RestClient client = new RestClient("url");  
      await client.ExecuteTaskAsync(request);

Thanks
Kailash Chandra Behera


No comments:

Post a Comment