Soda.Http 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Soda.Http --version 1.0.5
                    
NuGet\Install-Package Soda.Http -Version 1.0.5
                    
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="Soda.Http" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soda.Http" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Soda.Http" />
                    
Project file
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 Soda.Http --version 1.0.5
                    
#r "nuget: Soda.Http, 1.0.5"
                    
#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 Soda.Http@1.0.5
                    
#: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=Soda.Http&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Soda.Http&version=1.0.5
                    
Install as a Cake Tool

Soda.Http

基于HttpClient封装的 Http 请求库。如果有什么好想法,可以提 Issue 或者 Pr。

用法

1 预载

预载配置并不是必须的,但是有助于我们进行一些通用基础设置,例如Headers、Accept、BaseUrl等等。

配置BaseUrl之后,可以直接使用QSodaHttp.Uri()代替QSodaHttp.Url(),直接填写接口即可。

AspNetCore中:

services.AddSodaHttp(opts =>
{
    opts.BaseUrl = "http://localhost:8080/";
    opts.Accept = new[]
    {
        "application/json",
        "text/plain",
        "*/*"
    };
    opts.EnableCompress = false;
    opts.Headers = new[]{
        ("X-Ca-Test", "key")
    };
});

较为通用的写法,程序构建时:

QSodaHttp.AddSodaHttp(opts =>
{
    opts.BaseUrl = "http://localhost:8080/";
    opts.Accept = new[]
    {
        "application/json",
        "text/plain",
        "*/*"
    };
    opts.EnableCompress = false;
    opts.Headers = new[]{
        ("X-Ca-Test", "key")
    };
})

2 全局配置 Authentication

有时需要全局配置 Authentication,如果在代码中请求中独立配置了 Authentication 则会覆盖全局 Authentication

QSodaHttp.InitAuthentication("Bearer", "Values");

如果你是塞到 Header 里的这种做法

QSodaHttp.AddHeader("X-Ca-Key", "Values");

3 Http 请求

3.1 QSodaHttp

API 示例:

var result = await QSodaHttp.Url("https://www.baidu.com/")
    .Header("X-Ca-Key", "XXX")
    .Authentication("Bearer", "XXX")
    .Params(new { Id = "123456" })
    .Body(new { })
    // .Form(...)
    // .File(...)
    .PostAsync<string>();

简单示例:

// 配置BaseUrl
var services = new ServiceCollection();
services.AddSodaHttp(opts =>
{
    opts.EnableCompress = false;
    opts.BaseUrl = "http://localhost:5050/";
});
var res = await QSodaHttp.Uri("/Test/Get").Params(new { Id = "123456" }).GetAsync<object>();
var res = await QSodaHttp.Uri("/Test/TestGetResult").Params(new { Id = "123456", Ids = new[] { "123", "456" } }).GetAsync<object>();
var res = await QSodaHttp.Uri("/Test/Post").Body(new { Id = "123456", Ids = new[] { "123", "456" } }).PostAsync<object>();
var res = await QSodaHttp.Uri("/Test/PostResult")
        .Params(new { Id = "123456", Ids = new[] { "123", "456" } })
        .Body(new { Id = "123456", Ids = new[] { "123", "456" } })
        .PostAsync<object>();
var res = await QSodaHttp.Uri("/Test/Delete").Params(new { Id = "123456" }).DeleteAsync<object>();
var res = await QSodaHttp.Uri("/Test/DeleteResult").Params(new { Id = "123456", Ids = new[] { "123", "456" } }).DeleteAsync<object>();
var res = await QSodaHttp.Uri("/Test/Put").Params(new { Id = "123456" }).PutAsync<object>();
var res = await QSodaHttp.Uri("/Test/PutResult")
        .Body(new { Id = "123456", Ids = new[] { "123", "456" } })
        .PutAsync<object>();
var res = await QSodaHttp.Uri("Patch").Params(new { Id = "123456" }).PatchAsync<object>();
var res = await QSodaHttp.Uri("PatchResult")
        .Body(new { Id = "123456", Ids = new[] { "123", "456" } })
        .PatchAsync<object>();
Product 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 netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.

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.0.7 528 4/29/2023
1.0.6 390 4/2/2023
1.0.5 399 4/1/2023
1.0.5-preview 313 3/27/2023
1.0.5-beta 302 3/27/2023
1.0.4 406 3/25/2023
1.0.3 374 3/25/2023
1.0.2 377 3/25/2023
1.0.1 377 3/25/2023
1.0.0 350 3/25/2023