Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp simple complete example [closed]

Tags:

rest

c#

restsharp

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?)  * 
like image 497
Nil Pun Avatar asked Apr 19 '12 10:04

Nil Pun


People also ask

Is RestSharp open source?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.

What is RestSharp in C #?

RestSharp is a C# library used to build and send API requests, and interpret the responses. It is used as part of the C#Bot API testing framework to build the requests, send them to the server, and interpret the responses so assertions can be made.

How do I send a post request on RestSharp?

It's better to specify the Json data body. var client = new RestClient("URL"); var request = new RestRequest("Resources",Method. POST); request. RequestFormat = RestSharp.

Why should I use RestSharp?

We can say that RestSharp is probably the most popular HTTP client library for . NET among third-party libraries. Unlike HttpClient, RestSharp supports synchronous and asynchronous methods. Another great feature that RestSharp offers is automatic JSON, XML, and custom serialization and deserialization.


2 Answers

Changing

RestResponse response = client.Execute(request); 

to

IRestResponse response = client.Execute(request); 

worked for me.

like image 33
fractal Avatar answered Sep 22 '22 23:09

fractal


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

like image 102
wonea Avatar answered Sep 25 '22 23:09

wonea