RestSharp simple complete example

C#RestRestsharp

C# Problem Overview


I've been trying to create a simple prototype web application that uses RestSharp to call Rest API.

I've not been able to find one good example of it. Could anyone please share and direct me to right resource please? I've already looked at following, and doesn't provide what I'm looking for i.e fully functional example:

http://restsharp.org/ (Doesn't have full application with example)

http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/ (seems to be old)

While prototyping I get the error below for code below:

RestResponse response = client.Execute(request);

*Cannot implicitly convert type 'IRestResponse' to 'RestResponse'. An explicit conversion exists (are you missing a cast?)	*

C# Solutions


Solution 1 - C#

Pawel Sawicz .NET blog has a real good explanation and example code, explaining how to call the library;

GET:

var client = new RestClient("192.168.0.1");
var request = new RestRequest("api/item/", Method.GET);
var queryResult = client.Execute<List<Items>>(request).Data;

POST:

var client = new RestClient("http://192.168.0.1");
var request = new RestRequest("api/item/", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new Item
{
   ItemName = someName,
   Price = 19.99
});
client.Execute(request);

DELETE:

var item = new Item(){//body};
var client = new RestClient("http://192.168.0.1");
var request = new RestRequest("api/item/{id}", Method.DELETE);
request.AddParameter("id", idItem);
 
client.Execute(request)

The RestSharp GitHub page has quite an exhaustive sample halfway down the page. To get started install the RestSharp NuGet package in your project, then include the necessary namespace references in your code, then above code should work (possibly negating your need for a full example application).

NuGet RestSharp

Solution 2 - C#

Changing

RestResponse response = client.Execute(request);

to

IRestResponse response = client.Execute(request);

worked for me.

Solution 3 - C#

I managed to find a blog post on the subject, which links off to an open source project that implements RestSharp. Hopefully of some help to you.

http://dkdevelopment.net/2010/05/18/dropbox-api-and-restsharp-for-a-c-developer/ The blog post is a 2 parter, and the project is here: https://github.com/dkarzon/DropNet

It might help if you had a full example of what wasn't working. It's difficult to get context on how the client was set up if you don't provide the code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNil PunView Question on Stackoverflow
Solution 1 - C#woneaView Answer on Stackoverflow
Solution 2 - C#fractalView Answer on Stackoverflow
Solution 3 - C#pms1969View Answer on Stackoverflow