113

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?)  *
wonea
  • 4,783
  • 17
  • 86
  • 139
Nil Pun
  • 17,035
  • 39
  • 172
  • 294

3 Answers3

169

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

wonea
  • 4,783
  • 17
  • 86
  • 139
27

Changing

RestResponse response = client.Execute(request);

to

IRestResponse response = client.Execute(request);

worked for me.

Trevi Awater
  • 2,387
  • 2
  • 31
  • 53
fractal
  • 1,649
  • 17
  • 31
  • 3
    or `var response = client.Execute(request) as RestResponse;` – JohnTube Mar 27 '14 at 09:58
  • 1
    just to let anybody who lands here through a google search; changing to IRestResponse also helps with the typed calls. IRestResponse typedResponse = client.Execute(request); works as expected but RestResponse typedResponse = client.Execute(request); does not. – mahonya Jan 28 '15 at 11:49
  • Depends on what you are returned. for example in my case I'm using `response.Data` because i'm doing this `var response = Client.Execute>(request);` In which I return `return response.Data;` So for me I'm actually already using IRestResponse with var because if I was explicit it would be `IRestResponse>` for response - Otherwise the answer is correct ! just adding a comment – Tom Stickel Jul 22 '16 at 19:25
26

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.

pms1969
  • 3,354
  • 1
  • 25
  • 34
  • hi @pmms, basically I'm trying to following the code from here http://www.stum.de/2009/12/22/using-restsharp-to-consume-restful-web-services/ but I get the error which I described on my original question above. – Nil Pun Apr 19 '12 at 12:27
  • Sorry, can't reach it from inside the corporate network. I'll try looking later on. – pms1969 Apr 19 '12 at 15:32
  • 7
    OK, looking at the example, they use "var", you are using RestResponse. Try either using "var" or IRestResponse. They are also using a generic Execute. – pms1969 Apr 20 '12 at 14:34
  • Finally a decent example, cheers! –  Jun 22 '15 at 20:45
  • As it is now, this is a link-only answer. – Alex Nov 26 '15 at 09:20