Easy.HttpClient
1.1.8
dotnet add package Easy.HttpClient --version 1.1.8
NuGet\Install-Package Easy.HttpClient -Version 1.1.8
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Easy.HttpClient" Version="1.1.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Easy.HttpClient" Version="1.1.8" />
<PackageReference Include="Easy.HttpClient" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Easy.HttpClient --version 1.1.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Easy.HttpClient, 1.1.8"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Easy.HttpClient@1.1.8
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Easy.HttpClient&version=1.1.8
#tool nuget:?package=Easy.HttpClient&version=1.1.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Easy.HttpClient
it's a simple RESTful interface request.NET package.
It is very simple to use. Declare an attribute tag of [HttpClient] on the interface. During compilation, all interfaces containing [HttpClient] will be scanned to generate their implementation classes.
If the installed package still does not take effect, go to the project file (.csproj) and find
<PackageReference Include="Easy.HttpClient" />
and add OutputItemType="Analyzer", such as
<PackageReference Include="Easy.HttpClient" OutputItemType="Analyzer"/>
The methods defined in the interface are used to guide the generation of methods for implementing requests. You can use HTTP verb attribute tags to add tag declarations to methods <u>(only when tags such as [Get], [Post], [Put], [Delete], etc. are added will the code implementation part of the corresponding method be generated).</u> The parameters in the methods defined in the interface can also be added with attribute tags such as [Body], [Form], [Header], [Path], [Query], etc. to declare which part of HTTP the parameters belong to. The return value of the methods defined in the interface can be declared as any type. If it is a reference type, it will be converted into the corresponding class object according to the returned json for return. When the return value is declared as string, you can parse the returned result by yourself.
API Request Methods Definition
1. An example of a GET request is as follows:
[HttpClient("http://127.0.0.1/api")]
public interface TestClient
{
[Get("/search/{keyword}")]
public ApiResult Search([Path("keyword")]string keyword,[Query("page")]int page);
}
2. An example of a Post request is as follows:
[HttpClient("http://127.0.0.1/api")]
public interface TestClient
{
[Post("Add")]
public ApiResult AddNum([Form("count")]int count);
}
3. An example of a Put request is as follows:
[HttpClient("http://127.0.0.1/api")]
public interface TestClient
{
[Put("/put/{id}")]
public ApiResult Edit([Path("id")] Guid ida, [Body] int count);
}
4. An example of a Delete request is as follows:
[HttpClient("http://127.0.0.1/api")]
public interface TestClient
{
[Delete("/delete/{id}")]
public ApiResult Delete([Path] Guid id, [Query] int count, [Header("Authorization")]string token);
}
Invocation of API request methods
The format of the automatically generated code implementation class is the interface name suffixed with Impl, such as TestImpl.
TestClient testClient = new TestClientImpl();
var res = testClient.Search("cat",3);
res = testClient.AddNum(1);
res = testClient.Edit(Guid.NewGuid(), 4);
string token = "Bearer xxxxxx";
res = testClient.Delete(Guid.NewGuid(), 5, token);
If it is an ASP.NET WEB project, the defined client can be automatically registered through AddEasyHttpClient().It is registered as a service with a scoped lifecycle.If you want to call the method AddEasyHttpClient(), you must first import the namespace "Easy.HttpClient.Extensions".
[HttpClient("http://127.0.0.1/api")]
public interface EmployeeClient
{
[Get("employee/{eid}")]
public ApiResult GetEmployeeById([Path] Guid eid, [Header("Authorization")] string token);
}
using Easy.HttpClient.Extensions;//The namespace "Easy.HttpClient.Extensions" must be imported.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEasyHttpClient();//automatically registered
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly EmployeeClient _employeeClient;
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger, EmployeeClient employeeClient)
{
_logger = logger;
_employeeClient = employeeClient;
}
[HttpGet(Name = "GetWeatherForecast")]
public object Get()
{
Guid id = Guid.NewGuid();
string token = "Bearer xxxxxx";
var result = _employeeClient.GetEmployeeById(id, token);
return result;
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.8 | 185 | 12/15/2024 |
| 1.1.7 | 156 | 12/15/2024 |
| 1.1.5 | 175 | 12/13/2024 |
| 1.1.4 | 165 | 12/12/2024 |
| 1.1.3 | 155 | 12/12/2024 |
| 1.1.2 | 157 | 12/7/2024 |
| 1.1.1 | 156 | 12/5/2024 |
| 1.1.0 | 146 | 12/5/2024 |
| 1.0.9 | 158 | 12/3/2024 |
| 1.0.8 | 159 | 11/29/2024 |
| 1.0.7 | 146 | 11/28/2024 |
| 1.0.7-alpha-1 | 124 | 11/27/2024 |
| 1.0.6 | 161 | 10/10/2024 |
| 1.0.5 | 170 | 9/27/2024 |
| 1.0.4 | 178 | 9/25/2024 |
| 1.0.4-alpha-1 | 139 | 9/26/2024 |
| 1.0.3 | 164 | 9/24/2024 |
| 1.0.2 | 159 | 9/23/2024 |
| 1.0.2-alpha-2 | 149 | 9/23/2024 |
| 1.0.2-alpha-1 | 136 | 9/23/2024 |
Loading failed