RestSharp simple complete example [closed]
Asked Answered
A

3

114

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?)  *
Armilla answered 19/4, 2012 at 10:21 Comment(1)
@JohnSheehan looks like twillio uses HttpClient nor restsharpEnclasp
U
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.

Ulrica answered 19/4, 2012 at 10:31 Comment(5)
hi @pmms, basically I'm trying to following the code from here stum.de/2009/12/22/… but I get the error which I described on my original question above.Armilla
Sorry, can't reach it from inside the corporate network. I'll try looking later on.Ulrica
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.Ulrica
Finally a decent example, cheers!Personally
As it is now, this is a link-only answer.Morula
V
170

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

Vainglory answered 18/6, 2013 at 14:58 Comment(2)
This is missing parameter and headersAfflux
Your first GET example doesn't work: The type or namespace name 'List<>' could not be found The type or namespace name 'Items' could not be foundAttitude
P
27

Changing

RestResponse response = client.Execute(request);

to

IRestResponse response = client.Execute(request);

worked for me.

Possessory answered 27/3, 2013 at 8:51 Comment(3)
or var response = client.Execute(request) as RestResponse;Simple
just to let anybody who lands here through a google search; changing to IRestResponse also helps with the typed calls. IRestResponse<DummyData> typedResponse = client.Execute<DummyData>(request); works as expected but RestResponse<DummyData> typedResponse = client.Execute<DummyData>(request); does not.Plectron
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<List<Skill>>(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<List<Skill>> for response - Otherwise the answer is correct ! just adding a commentKauri
U
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.

Ulrica answered 19/4, 2012 at 10:31 Comment(5)
hi @pmms, basically I'm trying to following the code from here stum.de/2009/12/22/… but I get the error which I described on my original question above.Armilla
Sorry, can't reach it from inside the corporate network. I'll try looking later on.Ulrica
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.Ulrica
Finally a decent example, cheers!Personally
As it is now, this is a link-only answer.Morula

© 2022 - 2024 — McMap. All rights reserved.