Skip to content

Latest commit

 

History

History

README.md

🌊 Fluent.Client

A fluent HTTP client wrapper for .NET.

Build HTTP requests with a clean, chainable API. Less boilerplate, more productivity.

NuGet NuGet Downloads GitHub stars License

Created in Poland by Leszek Pomianowski and open-source community.


Table of Contents


🤔 Why This Library?

Traditional HttpClient usage requires verbose setup:

// ❌ Traditional approach - verbose and repetitive
var request = new HttpRequestMessage(HttpMethod.Post, "/api/users");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
request.Content = new StringContent(
    JsonSerializer.Serialize(new { Name = "John" }),
    Encoding.UTF8,
    "application/json");
var response = await client.SendAsync(request);

With Fluent.Client, requests become expressive one-liners:

// ✅ Fluent approach - clean and readable
var response = await client
    .Authorize(token: "abc123")
    .Post("/api/users", new { Name = "John" })
    .SendAsync();

⚡ Get Started

Install the Package

dotnet add package Fluent.Client

📦 NuGet: https://www.nuget.org/packages/Fluent.Client

Quick Example

using Fluent.Client;

var client = new HttpClient { BaseAddress = new Uri("https://api.example.com/") };

// Simple POST with JSON body
var response = await client
    .Post("/api/users", new { Name = "John Doe" })
    .SendAsync();

📖 How to Use

1. Create a Request

Start by creating a request using one of the HTTP method extensions on HttpClient.

using Fluent.Client;

var client = new HttpClient { BaseAddress = new Uri("https://api.example.com/") };

// POST with JSON body
var request = client.Post("/api/v1/users", new { Name = "John Doe" });

// GET with query parameters
var request = client.Get("/api/v1/users", new { page = 1, limit = 10 });

// DELETE
var request = client.Delete("/api/v1/users/897");

// PUT with body
var request = client.Put("/api/v1/users/897", new { Name = "Jane Doe" });

Available HTTP Methods

Method Description
.Get(path, query?) Create GET request with optional query parameters
.Post(path, body?) Create POST request with optional JSON body
.Put(path, body?) Create PUT request with optional JSON body
.Delete(path) Create DELETE request
.Patch(path, body?) Create PATCH request with optional JSON body

2. Configure the Request

Chain configuration methods to add headers, authorization, and more.

Authorization

// Bearer token
client.Authorize(token: "jwt-token-here").Get("/api/protected");

// Basic authentication
client.Authorize(username: "john", password: "secret").Get("/api/protected");

Query Parameters

// Pass as anonymous object
client.Get("/api/users", new 
{ 
    page = 1, 
    limit = 10, 
    sortBy = "createdAt" 
});

Chaining Multiple Configurations

var request = client
    .Authorize(token: "abc123")
    .Get("/api/v1/basket", new { includeItems = true });

3. Send the Request

Send the request and handle the response.

Get HttpResponseMessage
using HttpResponseMessage response = await request.SendAsync();

if (response.IsSuccessStatusCode)
{
    var content = await response.Content.ReadAsStringAsync();
    Console.WriteLine(content);
}
Deserialize Response Automatically
// Automatically deserialize JSON response to typed object
UserCreatedResponse result = await request.SendAsync<UserCreatedResponse>();

Console.WriteLine($"Created user: {result.Id}");
Direct Execution (No SendAsync)
// The request returns Task<HttpResponseMessage>, so you can await directly
using var response = await client
    .Authorize(token: "abc123")
    .Post("/api/users", new { Name = "John" });

📚 API Reference

Request Creation Methods

Method Description
Get(path, query?) Create GET request
Post(path, body?) Create POST request
Put(path, body?) Create PUT request
Delete(path) Create DELETE request
Patch(path, body?) Create PATCH request

Configuration Methods

Method Description
Authorize(token) Add Bearer token authorization
Authorize(username, password) Add Basic authentication

Execution Methods

Method Description
SendAsync() Send request and return HttpResponseMessage
SendAsync<T>() Send request and deserialize response to T

🧪 Testing with AwesomeAssertions

Pair this library with Fluent.Client.AwesomeAssertions for expressive test assertions:

await client
    .Authorize(token: "abc123")
    .Post("/api/users", new { Name = "John" })
    .Should()
    .Succeed("because valid user data was provided");

📦 Install: dotnet add package Fluent.Client.AwesomeAssertions


👥 Maintainers


💬 Support

For support, please open a GitHub issue. We welcome bug reports, feature requests, and questions.


📄 License

This project is licensed under the terms of the MIT open source license. Please refer to the LICENSE file for the full terms.

You can use it in private and commercial projects. Keep in mind that you must include a copy of the license in your project.