https://github.com/mynihongo/fluenthttp
Fluent wrapper around IHttpClientFactory
https://github.com/mynihongo/fluenthttp
fluent http httpclient httpclientfactory
Last synced: 5 months ago
JSON representation
Fluent wrapper around IHttpClientFactory
- Host: GitHub
- URL: https://github.com/mynihongo/fluenthttp
- Owner: MyNihongo
- License: mit
- Created: 2022-01-08T11:05:21.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-21T01:02:49.000Z (12 months ago)
- Last Synced: 2025-10-10T04:26:27.262Z (6 months ago)
- Topics: fluent, http, httpclient, httpclientfactory
- Language: C#
- Homepage:
- Size: 205 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/MyNihongo.FluentHttp/)
[](https://www.nuget.org/packages/MyNihongo.FluentHttp/)
# FluentHttp
Fluent wrapper around IHttpClientFactory
Install a NuGet package `MyNihongo.FluentHttp`.
## Configuration
Add a section to `IConfiguration`
```json
{
"FluentHttp": {
"BaseAddress": "https://jsonplaceholder.typicode.com",
"NtlmEnabled": false,
"Timeout": 100
}
}
```
- `Timeout` in seconds. for `Timeout.Infinite` pass -1.
Register a service
```cs
using MyNihongo.FluentHttp;
services.AddFluentHttp();
// Or optionally configure the HTTP client
services.AddFluentHttp((services, httpClient) =>
{
// configure
});
```
## HTTP methods
To optimize JSON serialization `JsonTypeInfo` can be supplied for all methods. More info about these types [here](https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/).
In further examples a variable `IFluentHttp fluentHttp` will be used.
#### GetJsonAsync
Gets a JSON stream.
```cs
[JsonSerializable(typeof(RecordContext[]))]
internal partial class RecordContext : JsonSerializerContext {}
public sealed record RecordContext
{
public int Id { get; set; }
}
// Get the model
var models = await fluentHttp
.AppendPathSegment("example")
.GetJsonAsync(RecordContext.Default.RecordArray, ct);
// When an API may return non-JSON values (e.g. `""`) this method can be used in order to ignore parsing exceptions
fluentHttp.GetJsonOrDefaultAsync(RecordContext.Default.RecordArray, ct);
```
#### PostJsonAsync
Posts a JSON model and gets the JSON response.
```cs
[JsonSerializable(typeof(Request))]
internal partial class RequestContext : JsonSerializerContext {}
[JsonSerializable(typeof(Response))]
internal partial class ResponseContext : JsonSerializerContext {}
var req = new Request
{
Data = "example"
};
var response = await fluentHttp
.AppendPathSegment("example")
.PostJsonAsync(req, RequestContext.Default.Request, ResponseContext.Default.Response, ct);
// When an API may return non-JSON values (e.g. `""`) this method can be used in order to ignore parsing exceptions
fluentHttp.PostJsonOrDefaultAsync(req, RequestContext.Default.Request, ResponseContext.Default.Response, ct);
```
## Fluent extensions
Fluent extensions supply additional parameters for the main HTTP methods.
#### AppendPathSegment
Appends a new section to the request URI.
```cs
// get from https://jsonplaceholder.typicode.com/posts
var result = await fluentHttp
.AppendPathSegment("posts")
.GetJsonAsync();
```
#### AppendPathSegments
Appends multiple sections to the request URI.
```cs
// get from https://jsonplaceholder.typicode.com/posts/1/comments
var result = await fluentHttp
.AppendPathSegment("posts", "1", "comments")
.GetJsonAsync();
```
#### AppendParameter
Appends a URL parameter
```cs
// get from https://jsonplaceholder.typicode.com/posts?value=123
var result = await fluentHttp
.AppendPathSegment("posts")
.AppendParameter("value", "123")
.GetJsonAsync();
```
#### WithHeader
Appends a header to the request.
```cs
var result = await fluentHttp
.AppendPathSegment("posts")
.WithHeader("my-header", "value")
.GetJsonAsync();
```
#### Basic authentication
Append the authentication header for the basic authentication
```cs
var result = await fluentHttp
.AppendPathSegment("posts")
.WithBasicAuth("username", "strong password")
.GetJsonAsync();
```