DalSoft C# RestClient
If you find this repo / package useful please star it.
Just some of the things you can do with DalSoft.RestClient
- Easliy Create Fluent SDK's
- Unit Testing
- Post Json
- Post Forms
- Post Files
- Retry Requests
- Twitter SDK
- Raw HTTP
- Passthrough HttpClient
RestClient targets .NET Standard 2.0 therefore supports Windows, Linux, Mac and Xamarin (iOS, Android and UWP).
If you need to target .NET Standard 1.4 use version 3.2.2.
Install via .NET CLI
> dotnet add package DalSoft.RestClient
Install via NuGet
PM> Install-Package DalSoft.RestClient
Example calling a REST API
You start by new'ing up the RestClient and passing in the base uri for your RESTful API.
For example if your wanted to perform a GET on https://jsonplaceholder.typicode.com/users/1 you would do the following:
Static Typed Rest Client
For the Static typed Rest Client just pass a string representing the resource you want access to the Resource method, and then call the HTTP method you want to use.
var client = new RestClient("https://jsonplaceholder.typicode.com"); User user = await client.Resource("users/1").Get(); Console.WriteLine(user.Name);
Dynamicaly Typed Rest Client
For the Dynamicaly typed Rest Client 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
- Version 4.0 Static Typing and Resource Expressions
- Version 3.3.0 IHttpClientFactory Goodness
- Version 3.0 Pipeline Awesomeness
RestClient is a very lightweight wrapper around System.Net.HttpClient that uses the dynamic features of .NET 4 to provide a fluent way of accessing RESTFul API's, making it trivial to create REST requests using a lot less code.
Originally created to remove the boilerplate code involved in making REST requests using code that is testable. I know there are a couple of REST clients out there but I wanted the syntax to look a particular way with minimal fuss.
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.
Standing on the Shoulders of Giants
DalSoft.RestClient is built using the following great open source projects:
DalSoft.RestClient is inspired by and gives credit to: