Click here to Skip to main content
15,559,275 members
Articles / Web Development / HTML
Technical Blog
Posted 12 Aug 2015

Tagged as

Stats

20.2K views
4 bookmarked

Calling an API using RestSharp in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
12 Aug 2015CPOL
Calling an API using RestSharp in ASP.NET

These days, Rest Services are most popular, everyone likes to use Rest Services. RestSharp is a Simple REST and HTTP client for .NET.

There are many ways to consume RESTful services in client veg. WebClient, etc. But, RestSharp is wonderful to consume REST services. This provides a simple call while we want to consume services at client side.

This is easier just in three steps:

Create RestClient

C#
private readonly RestClient _client;  
private readonly string _url = ConfigurationManager.AppSettings["webapibaseurl"];  
  
public ServerDataRestClient()  
{  
   _client = new RestClient(_url);  
}

In the above snippet, we created a client with Base url of our services and retrieving this url from our config file (a recommended way). Also, we can use the same as follows (not recommended):

C#
var client = new RestClient("http://crudwithwebapi.azurewebsites.net/"); 

Make a Request

C#
var request = new RestRequest("api/serverdata", Method.GET) {RequestFormat = DataFormat.Json};

Here, we are calling GET resource and telling RestSharp to provide data/response in JSON format.

Play with Response

C#
var response = _client.Execute<List<ServerDataModel>>(request);

The complete code would look like:

C#
private readonly RestClient _client;  
private readonly string _url = ConfigurationManager.AppSettings["webapibaseurl"];  
  
public ServerDataRestClient()  
{  
   _client = new RestClient(_url);  
}  
  
public IEnumerable<ServerDataModel> GetAll()  
{  
    var request = new RestRequest("api/serverdata", Method.GET) {RequestFormat = DataFormat.Json};  
  
    var response = _client.Execute<List<ServerDataModel>>(request);  
  
     if (response.Data == null)  
          throw new Exception(response.ErrorMessage);  
  
     return response.Data;  
}

This is just a simple implementation, we can create a generic class and use the same in an abstract way.

The post Calling an API using RestSharp in ASP.NET appeared first on Gaurav-Arora.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
There is no age limit for learning and its never late, to start.

Comments and Discussions

 
QuestionSource code? Pin
Shuby Arora12-Aug-15 21:15
Shuby Arora12-Aug-15 21:15 
AnswerRe: Source code? Pin
Gaurav Aroraa13-Aug-15 5:13
professionalGaurav Aroraa13-Aug-15 5:13 
I will update meanwhile you can get it from https://github.com/garora

SuggestionPlace this in right category Pin
Shuby Arora12-Aug-15 21:13
Shuby Arora12-Aug-15 21:13 
GeneralRe: Place this in right category Pin
Gaurav Aroraa13-Aug-15 5:14
professionalGaurav Aroraa13-Aug-15 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.