Skip to content

Quick Start & Samples

Christian Findlay edited this page May 8, 2020 · 8 revisions

Clone this repo and open the samples solution. There are samples for WebAssembly (in browser, UWP, Android, iOS, .NET Core, and many examples of common tasks like Polly Integration in the Unit Tests). The solution is called RestClient.Net.Samples.sln. Server side Blazor samples can be found here.

or

NuGet: Install-Package RestClient.NET

Please read this article about Serialization and Deserialization.

With Newtonsoft serialization

var client = new Client(new NewtonsoftSerializationAdapter(), new Uri("https://restcountries.eu/rest/v2/"));
var response = await client.GetAsync<List<RestCountry>>();

With default serialization on .NET Core

var client = new Client(new Uri("https://restcountries.eu/rest/v2/"));
var response = await client.GetAsync<List<RestCountry>>();

Post / Put / Patch

Protocol Buffers (Binary)

var person = new Person { FirstName = "Bob", Surname = "Smith" };
var client = new Client(new ProtobufSerializationAdapter(), new Uri("http://localhost:42908/person"));
person = await client.PostAsync<Person, Person>(person);

JSON

var client = new Client(new NewtonsoftSerializationAdapter(), new Uri("https://jsonplaceholder.typicode.com"));
client.SetJsonContentTypeHeader();
UserPost userPost = await client.PostAsync<UserPost, UserPost>(new UserPost { title = "Title" }, "/posts");
var client = new Client(new NewtonsoftSerializationAdapter(), new Uri("https://jsonplaceholder.typicode.com"));
await client.DeleteAsync("posts/1");
var baseUri = new Uri("https://www.google.com");
var client = new Client(new NewtonsoftSerializationAdapter(), baseUri);
var response = await client.SendAsync<string, object>(new Request<object>(
    null,
    null,
    null,
    HttpRequestMethod.Custom,
    client,
    default)
{ CustomHttpRequestMethod = "HEAD" });