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?)  *
1
  • @JohnSheehan looks like twillio uses HttpClient nor restsharp Commented Mar 23, 2018 at 18:01

3 Answers 3

172

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is missing parameter and headers
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 found
28

Changing

RestResponse response = client.Execute(request);

to

IRestResponse response = client.Execute(request);

worked for me.

3 Comments

or var response = client.Execute(request) as RestResponse;
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.
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 comment
27

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.

5 Comments

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.
Sorry, can't reach it from inside the corporate network. I'll try looking later on.
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.
Finally a decent example, cheers!
As it is now, this is a link-only answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.