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 over to the 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.
And you’re never locked in - RestClient is a lightweight wrapper around HttpClient, so you can drop down to the underlying HttpClient at any time via client.HttpClient with everything you’ve configured still applied. See Passthrough HttpClient.
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.
Statically typed - use the Resource method to access the resource you want - ending with the HTTP method you want to use.
For example if you wanted to perform a GET on https://jsonplaceholder.typicode.com/users/1 you would do the following:
var client = new RestClient("https://jsonplaceholder.typicode.com");
User user = await client.Resource("users/1").Get();
Console.WriteLine(user.Name);
Dynamically typed - simply chain members that would make up the resource you want to access - ending with the HTTP method you want to use.
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
Using dependency injection
In ASP.NET Core register a RestClient with AddRestClient() and inject IRestClientFactory, or register a typed client for your API with AddRestClient<TClient>() - both are backed by IHttpClientFactory:
builder.Services.AddRestClient("https://api.github.com", new Headers(new { UserAgent = "MyClient" }));
builder.Services.AddRestClient<GitHubClient>("https://api.github.com");
See IHttpClientFactory and Typed Clients for the details.
Next steps
- Static Typing - declare your client as
RestClientorvarinstead ofdynamicfor full IntelliSense. - McpHandler - call MCP (Model Context Protocol) servers like any other API.
- Upgrading to 5.0 and Performance - what changed in 5.0 and the numbers.