Quick-Start Guide

RestClient is biased towards posting and returning JSON - if you don’t provide Accept and Content-Type headers then they are set to application/json by default See Working with non JSON content.

That said over the years RestClient has evolved to do almost anything with HttpClient using Handlers. For example RestClient supports x-www-form-urlencoded and multipart/form-data making it trivial to post forms and files.

Head of over to Available Handlers section to see what’s Available, or Handler Pipeline to see how easy it is to configure your own handlers to do anything with RestClient.

Install via .NET CLI

> dotnet add package DalSoft.RestClient

Install via NuGet

PM> Install-Package DalSoft.RestClient

Example call a REST API in two lines of code

You start by new’ing up the RestClient and passing in the base uri for your RESTful API.

Then simply chain members that would make up the resource you want to access - ending with the HTTP method you want to use.

For example if your wanted to perform a GET on https://jsonplaceholder.typicode.com/users/1 you would do the following:

dynamic client = new RestClient("https://jsonplaceholder.typicode.com");

var user = await client.Users(1).Get();
   
Console.WriteLine(user.name);

Note all HTTP methods are async

New in Version 3.3.0 IHttpClientFactory Goodness!

In version 3.3.0 we added support for .NET Core’s 2.1 IHttpClientFactory.

First register your RestClient as a service in Startup.cs.

public class Startup
{
   public void ConfigureServices(IServiceCollection services)
   {
      services.AddRestClient("https://api.github.com", new Headers(new { UserAgent = "MyClient" }));
   }
}

Then inject IRestClientFactory into your controller, which is how we support IHttpClientFactory.

public class GitHubController : Controller
{
   private readonly IRestClientFactory _restClientFactory;
        
   public GitHubController(IRestClientFactory restClientFactory)
   {
      _restClientFactory = restClientFactory;
   }

   [Route("github/users/dalsoft"), HttpGet]
   public async Task<List<Repository>> CreateClient()
   {
      dynamic restClient = _restClientFactory.CreateClient();
            
      var repositories = await restClient.users.dalsoft.repos.Get();
            
      return repositories;
   }
}

See IHttpClientFactory for more examples and details on our IHttpClientFactory support.