RestEase
Easy-to-use typesafe REST API client library for .NET Standard 1.1 and .NET Framework 4.5 and higher, which is simple and customisable. Inspired by Refit
Install / Use
/learn @canton7/RestEaseREADME
RestEase
RestEase is a little type-safe REST API client library for .NET Framework 4.5.2 and higher and .NET Platform Standard 1.1 and higher, which aims to make interacting with remote REST endpoints easy, without adding unnecessary complexity.
To use it, you define an interface which represents the endpoint you wish to communicate with (more on that in a bit), where methods on that interface correspond to requests that can be made on it. RestEase will then generate an implementation of that interface for you, and by calling the methods you defined, the appropriate requests will be made. See Installation and Quick Start to get up and running!
Almost every aspect of RestEase can be overridden and customized, leading to a large level of flexibility.
It also works on platforms which don't support runtime code generation, such as .NET Native and iOS, if you reference RestEase.SourceGenerator. See Using RestEase.SourceGenerator for more information.
RestEase is built on top of HttpClient and is deliberately a "leaky abstraction": it is easy to gain access to the full capabilities of HttpClient, giving you control and flexibility, when you need it.
RestEase is inspired by Anaïs Betts' Refit, which in turn is inspired by Retrofit.
Table of Contents
- Installation
- Quick Start
- Request Types
- Return Types
- Query Parameters
- Paths
- Body Content
- Response Status Codes
- Cancelling Requests
- Headers
- Using RestEase.SourceGenerator
- Using HttpClientFactory
- Using RestEase with Polly
- HttpClient and RestEase interface lifetimes
- Controlling Serialization and Deserialization
- Controlling the Requests
- Customizing RestEase
- Interface Accessibility
- Using Generic Interfaces
- Using Generic Methods
- Interface Inheritance
- Advanced Functionality Using Extension Methods
- FAQs
Installation
RestEase is available on NuGet. See that page for installation instructions.
If you're using C# 9 or .NET 5 (or higher), reference RestEase.SourceGenerator as well to get compile-time errors and faster execution. See Using RestEase.SourceGenerator for more information. If you're targetting iOS or .NET Native, you will need to do this, as runtime code generation isn't available.
If you're using ASP.NET Core, take a look at Using HttpClientFactory. For failure handling and retries using Polly, see Using RestEase with Polly.
Quick Start
To start, first create an public interface which represents the endpoint you wish to make requests to. Please note that it does have to be public, or you must add RestEase as a friend assembly, see Interface Accessibility below.
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestEase;
namespace RestEaseSampleApplication
{
// We receive a JSON response, so define a class to deserialize the json into
public class User
{
public string Name { get; set; }
public string Blog { get; set; }
// This is deserialized using Json.NET, so use attributes as necessary
[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }
}
// Define an interface representing the API
// GitHub requires a User-Agent header, so specify one
[Header("User-Agent", "RestEase")]
public interface IGitHubApi
{
// The [Get] attribute marks this method as a GET request
// The "users" is a relative path the a base URL, which we'll provide later
// "{userId}" is a placeholder in the URL: the value from the "userId" method parameter is used
[Get("users/{userId}")]
Task<User> GetUserAsync([Path] string userId);
}
public class Program
{
public static void Main(string[] args)
{
// Create an implementation of that interface
// We'll pass in the base URL for the API
IGitHubApi api = RestClient.For<IGitHubApi>("https://api.github.com");
// Now we can simply call methods on it
// Normally you'd await the request, but this is a console app
User user = api.GetUserAsync("canton7").Result;
Console.WriteLine($"Name: {user.Name}. Blog: {user.Blog}. CreatedAt: {user.CreatedAt}");
Console.ReadLine();
}
}
}
Request Types
See the [Get("path")] attribute used above? That's how you mark that method as being a GET request. There are a number of other attributes you can use here - in fact, there's one for each type of request: [Get("path")], [Post("path")], [Put("path")], [Delete("path")], [Head("path")], [Options("path")], [Trace("path"))], [Patch("path")]. Use whichever one you need to.
The argument to [Get] (or [Post], or whatever) is typically a relative path, and will be relative to the base uri that you provide to RestClient.For<T>. (You can specify an absolute path here if you need to, in which case the base uri will be ignored). Also see the section on Paths.
Return Types
Your interface methods may return one of the following types:
Task: This method does not return any data, but the task will complete when the request has completedTask<T>(whereTis not one of the types listed below): This method will deserialize the response into an object of typeT, using Json.NET (or a custom deserializer, see [Controlling Serialization and
Related Skills
node-connect
338.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
338.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.6kCommit, push, and open a PR
