How do I make a API calls using RestSharp

There are many ways to consume RESTFull services in client veg. WebClient etc. But, RestSharp is the wonderful to consule REST services. This provides a simple call while we want o consume services at client side.
 
This is easier just in three steps:
 
1. Create RestClient
 
  1. private readonly RestClient _client;  
  2. private readonly string _url = ConfigurationManager.AppSettings["webapibaseurl"];  
  3.   
  4. public ServerDataRestClient()  
  5. {  
  6.    _client = new RestClient(_url);  
  7. }  
 In above, snippet we created a client with Base url of our services and retrieveing this url from our config file (a recommended way). Also, we can use the same as followings (not recommended)
 
  1. var client = new RestClient("http://crudwithwebapi.azurewebsites.net/");  
 
2. Make a request
 
  1. 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.
 
3. Play with response 
 
  1. var response = _client.Execute<List<ServerDataModel>>(request);  
Here, we are getting response :)
 
Complete code would look like:
 
  1. private readonly RestClient _client;  
  2. private readonly string _url = ConfigurationManager.AppSettings["webapibaseurl"];  
  3.   
  4. public ServerDataRestClient()  
  5. {  
  6.    _client = new RestClient(_url);  
  7. }  
  8.   
  9. public IEnumerable<ServerDataModel> GetAll()  
  10. {  
  11.     var request = new RestRequest("api/serverdata", Method.GET) {RequestFormat = DataFormat.Json};  
  12.   
  13.     var response = _client.Execute<List<ServerDataModel>>(request);  
  14.   
  15.      if (response.Data == null)  
  16.           throw new Exception(response.ErrorMessage);  
  17.   
  18.      return response.Data;  
  19. }  
 This is just a simple implementation, we can create a generic class and use the same in abstract way.