in c#

Introduction to RESTSharp

I would like to show you, good library to working with REST architecture. Nowadays REST is getting more popular because it’s very simple API, even ASP.NET MVC support this as default API service.

RESTSharp, is powerful library for any kind of .NET technology. Especially for Windows Phone where you have to use REST or SOAP to communicate with your external data. I came across RESTSharp when I was working on Project for Imagine Cup 2013. It saves me, a lot of time. RestSharp support asynchronously actions as well as windows phone because it is the main presumption of programming on that platform. I would like to show you few code block of using RESTSharp. At the begging basics and then in the next blog post some advance methods of using library with async.

First, we have to make a connection with server where API is located. Then we add physical path to specific API controller, and choose method of connection (GET, POST, DELETE, PUT etc.). Next step is create a query to our REST API services, our result will be list of Item, it’s highly recommended to use that type deserialization, now we can easily get access just adding .Data at the end of statement.

Now let’s look at Method.POST.

client and request stay same like in first example, but there is one change in method of connection, please set Method.POST. Next step is add body to our request create model that is expected in API controller, and finally last line, execute our request.

If we want run delete method, just use Method.DELETE

Declaring client stay same like in other cases, request has small change, first you have to add path with parameter and define method of connection Method.GET. Moreover, we have to add to request one more property, define parameter {id} and what values its get. Finally execute request.

  • Bartek Sarul

    I’ve just finished a project using RestSharp and had one major issue – RestClient is not thread safe. Here are the details:

    https://groups.google.com/forum/#!msg/restsharp/X8JPtcfnSlo/ThLpBOKYgRcJ

    Strange that it has been more than a year since this has been noticed.

    • http://pawel.sawicz.eu/ Pawel Sawicz

      RESTSharp is open source so you can fix it ;). Thanks for reply and we must have faith that it will be fixed someday.

  • Pingback: Async and RESTSharp | Pawel Sawicz .NET blog

  • vijay kapoor

    String input1 = “103413”;

    var client = new RestClient(“http://164.100.128.8/mFMS/services/WholesalerStockAvailability”);

    var request = new RestRequest(Method.POST);

    request.AddHeader(“Content-Type”, “application/xml”);

    request.AddParameter(“application/xml”, input1, ParameterType.RequestBody);

    //RestResponse response = (RestResponse)client.Execute(request);

    //var content = response.Content; // raw content as string

    //Response.Write(content);

    //IRestResponse response = client.Execute(request);

    //var content = response.Content; // raw content as string

    //Response.Write(content+”-“);

    IRestResponse response1 = client.Execute(request);

    var wholesalerId = response1.Data.wholesalerId;

    Response.Write(wholesalerId + “-“);

    IRestResponse response2 = client.Execute(request);

    var companyId = response2.Data.companyId;

    Response.Write(companyId + “-“);

    IRestResponse response3 = client.Execute(request);

    var productId = response3.Data.productId;

    Response.Write(productId + “-“);

    IRestResponse response4 = client.Execute(request);

    var quantity = response4.Data.quantity;

    Response.Write(quantity + “-“);

    IRestResponse response5 = client.Execute(request);

    var plantId = response5.Data.plantId;

    Response.Write(plantId + “—=”);

    how to get second record in this code plz help me

    • http://pawel.sawicz.eu/ Pawel Sawicz

      Hey,

      You can set a List of wholesaler.

      IRestResponse response = client.Execute<List>(request);

      Now you will get a list with objects of wholesaler type.

  • indrit saveta

    Hello i have problem with rest sharp . I want to send data json format with post method and to take json format response (xamarin android client) …..can you help me ?This is a code i try and show status code 0 …but from fiddler work ….void Testim()
    void Testimi();
    {

    var client = new RestClient(“https://x-hp/api/Test1/”);

    var request = new RestRequest(“/”,Method.POST);

    request.AddHeader(“Content-Type”,”application/json”);

    request.RequestFormat = RestSharp.DataFormat.Json;

    request.RequestFormat = RestSharp.DataFormat.Json;

    var pergj = new Pergjigje();

    pergj.Password = “12345”;

    pergj.Username = “Test”;

    String paramJson = JsonConvert.SerializeObject(pergj);

    request.AddBody(paramJson);

    request.AddParameter(“Application/Json”, paramJson, ParameterType.RequestBody);

    client.ExecuteAsync (request,response => {

    RunOnUiThread(delegate {

    Toast.MakeText(this,response.StatusCode.ToString(), ToastLength.Long).Show();

    });

    });

  • indrit saveta

    Hello i have problem with rest sharp . I want to send data json format with post method and to take json format response (xamarin android client) …..can you help me ?This is a code i try and show status code 0 …but from fiddler work ….void Testim()

    void Testimi();

    {

    var client = new RestClient(“https://x-hp/api/Test1/”);

    var request = new RestRequest(“/”,Method.POST);

    request.AddHeader(“Content-Type”,”application/json”);

    request.RequestFormat = RestSharp.DataFormat.Json;

    request.RequestFormat = RestSharp.DataFormat.Json;

    var pergj = new Pergjigje();

    pergj.Password = “12345”;

    pergj.Username = “Test”;

    String paramJson = JsonConvert.SerializeObject(pergj);

    request.AddBody(paramJson);

    request.AddParameter(“Application/Json”, paramJson, ParameterType.RequestBody);

    client.ExecuteAsync (request,response => {

    RunOnUiThread(delegate {

    Toast.MakeText(this,response.StatusCode.ToString(), ToastLength.Long).Show();

    });

    });

    Photo o Fidleri ..method post and url and body json format …..

  • Xavier Baquero-Dirani

    Hello,
    Please some help with the HttpBasicAuthenticator sentence. Visual Studio 2015 Community says: Severity Code Description Project File Line Error CS0246 The type or namespace name ‘HttpBasicAuthenticator’ could not be found (are you missing a using directive or an assembly reference?)

    (RestSharp 105.2.3)

    RestClient client = new RestClient();
    client.BaseUrl = new Uri(“https://api.mailgun.net/v3”); // del manual de RestSharp client.BaseUrl = new Uri(“http://twitter.com”);
    client.Authenticator = new HttpBasicAuthenticator(“api”,”key-7xyz”);

    • http://pawel.sawicz.eu/ Pawel Sawicz

      probably you are missing using

    • WnLoco

      include:
      using RestSharp.Authenticators;