Giter Club home page Giter Club logo

restsharp's Introduction

RestSharp - Simple .NET REST Client

Build status

Join the chat at https://gitter.im/RestSharp/RestSharp

Official Site - @RestSharp - Google Group

License: Apache License 2.0

Note on RestSharp.Signed

The RestSharp package is now signed so there is no need to install RestSharp.Signed, which is obsolete from v160.0.0.

Note on JSON serialization

Some time ago, we have decided to get rid of the reference to Newtonsoft.Json package. The intentions were good, we thought that the SimpleJson library would be a good replacement that can be embedded to the library itself, so we don't need to have any external references.

However, as many good intentions, that change created more issues than it solved. The number of issues on GitHub that are related to JSON (de)serialization is growing and SimpleJson is long abandoned. We faced a choice to start maintaining SimpleJson ourselves or use something else.

Since as per today almost every .NET project has a direct or indirect reference to Newtonsoft.Json, we decided to bring it back as a dependency and get rid of SimpleJson. This will be done in RestSharp v107, the next major version.

To prepare for this change, we made quite a few changes in how serialization works in RestSharp. Before, objects were serialized when added to the RestRequest by using one of the AddBody methods. That made it impossible to assign a custom serializer on the client level, so it should have been done for each request. In v106.6 body parameter is serialized just before executing the request. Delaying the serialization allowed us to add the client-level serializer.

It is still possible to assign custom (de)serializer per request, as before. In addition to that, you can use the new method IRestClient.UseSerializer(IRestSerializer restSerializer). The IRestSerializer interface has methods for serialization and deserialization. Default serializers are the same as before.

From v106.6.2 you can use Newtonsoft.Json for the RestClient by using code from this snippet.

In addition to that, you can change the default XML serialization to use the System.Xml serializer, also known as DotNetXmlSerializer and DotNetXmlDeserializer. In v106.6.2, you can simply write:

client.UseDotNetXmlSerializer();

Features

  • Assemblies for .NET 4.5.2 and .NET Standard 2.0
  • Easy installation using NuGet for most .NET flavors (signed)
  • Automatic XML and JSON deserialization
  • Supports custom serialization and deserialization via ISerializer and IDeserializer
  • Fuzzy element name matching ('product_id' in XML/JSON will match C# property named 'ProductId')
  • Automatic detection of type of content returned
  • GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, COPY supported
  • Other non-standard HTTP methods also supported
  • OAuth 1, OAuth 2, Basic, NTLM and Parameter-based Authenticators included
  • Supports custom authentication schemes via IAuthenticator
  • Multi-part form/file uploads
var client = new RestClient("http://example.com");
// client.Authenticator = new HttpBasicAuthenticator(username, password);

var request = new RestRequest("resource/{id}");
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource

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

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

// easily add HTTP Headers
request.AddHeader("header", "value");

// add files to upload (works with compatible verbs)
request.AddFile("file", path);

// execute the request
var response = client.Post(request);
var content = response.Content; // raw content as string

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

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

// easy async support
await client.ExecuteAsync(request);

// async with deserialization
var asyncHandle = client.PostAsync<Person>(request, response => {
    Console.WriteLine(response.Data.Name);
});

// abort the request on demand
asyncHandle.Abort();

restsharp's People

Contributors

alexeyzimarev avatar ayoung avatar brettryan avatar davidgrupp avatar devatwork avatar dixonds avatar dkarzon avatar eamonhetherton avatar erikschierboom avatar friism avatar haacked avatar hallem avatar issafram avatar jnyrup avatar johnsheehan avatar jsclayton avatar lsolano avatar mattleibow avatar mstum avatar naliath avatar pedrolamas avatar perlun avatar petejohanson avatar pseudomuto avatar psypher9 avatar rmblstrp avatar scidec avatar scottschluer avatar sgrassie avatar sinankeskin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.