Skip to content

ebello/RestSharp

 
 

Repository files navigation

RestSharp - .NET REST Client That (Hopefully) Doesn't Suck

License: Apache License 2.0

Key Features

  • Automatic XML and JSON deserialization
  • Fuzzy name matching ('product_id' in XML/JSON will match property named 'ProductId')
  • Automatic detection of type of content returned
  • GET, POST, PUT, HEAD, OPTIONS, DELETE supported
  • Basic, NTLM and Parameter-based Authenticators included
  • Supports custom authentication schemes
  • Multi-part form/file uploads
  • T4 Helper to generate C# classes from an XML document

Basic Usage

var client = new RestClient();
client.BaseUrl = "http://example.com";
// client.Authenticator = new BasicAuthenticator(username, password);

var request = new RestRequest(); // GET by default
// request.Method = Method.GET | Method.POST | Method.PUT | Method.DELETE | Method.HEAD | Method.OPTIONS
request.Resource = "resource";
request.AddParameter("name", "value");

// add parameters for all properties on an object
request.AddObject(object);

// or just whitelisted properties
request.AddObject(object, "PersonId", "Name", ...);

// easily add HTTP Headers
request.AddParameter("header", "value", ParameterType.HttpHeader);

// supports XML/JSON request bodies
request.RequestFormat = RequestFormat.Xml;
request.AddBody(object);

// add files (only works with compatible verbs)
request.AddFile(path);
    
// get raw response
RestResponse response = client.Execute(request);
// response.Content : string representation of response

// or automatically deserialize result
// return content type is sniffed but can be explicitly set via RestClient.AddHandler();
RestResponse<Person> response2 = client.Execute<Person>(request);
var name = response2.Data.Name;

// or download and save file to disk
client.DownloadData(request).SaveAs(path);

// shortcuts for parsing xml/feeds
client.ExecuteAsXDocument(request);
client.ExecuteAsXmlDocument(request);
client.ExecuteAsSyndicationFeed(request);

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%