DHttpClient.Core 1.0.2

dotnet add package DHttpClient.Core --version 1.0.2
                    
NuGet\Install-Package DHttpClient.Core -Version 1.0.2
                    
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="DHttpClient.Core" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DHttpClient.Core" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="DHttpClient.Core" />
                    
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 DHttpClient.Core --version 1.0.2
                    
#r "nuget: DHttpClient.Core, 1.0.2"
                    
#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 DHttpClient.Core@1.0.2
                    
#: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=DHttpClient.Core&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=DHttpClient.Core&version=1.0.2
                    
Install as a Cake Tool

DHttpClient - A Fluent HTTP Request Builder for .NET

DHttpClient is a flexible and fluent HTTP request builder for .NET applications. It simplifies making HTTP requests by providing an intuitive API for configuring requests, handling responses, and managing various content types.

Features

  • Fluent API: Easily build HTTP requests with a chainable, readable syntax.
  • Supports All HTTP Methods: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.
  • Flexible Content Handling:
    • JSON serialization and deserialization
    • Form URL-encoded content
    • Multipart form-data for file uploads
  • Automatic Query Parameter Handling
  • Customizable Headers and Timeout Settings
  • Error Handling with Unified Response Wrapper
  • Supports Custom HttpClient for DI compatibility
  • Result<T> Wrapper for uniform success/error handling
  • Stream and Byte Array Support for file downloads
  • Custom HTTP Headers for Requests and Content
  • Supports Multipart Requests for handling multiple parts of data
  • Custom HTTP Timeout Configuration

Installation

Add the package reference to your .NET project:

Install-Package DHttpClient

Or, using .NET CLI:

dotnet add package DHttpClient

Usage

Basic GET Request

using DHttpClient;

var request = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/get")
    .WithMethod(HttpMethod.Get);

var response = await request.SendAsync();

if (response.IsSuccess)
{
    var content = await response.Data.Content.ReadAsStringAsync();
    Console.WriteLine(content);
}
else
{
    Console.WriteLine($"Error: {response.ErrorMessage}");
}

GET Request with Query Parameters

var request = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/get")
    .WithQueryParameters(new { user = "JohnDoe", age = 30 })
    .WithMethod(HttpMethod.Get);

POST Request with JSON Payload

var payload = new { name = "John", email = "john@example.com" };

var request = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/post")
    .WithMethod(HttpMethod.Post)
    .WithBodyContent(payload);

POST Request with Form URL-Encoded Content

var formData = new Dictionary<string, string>
{
    { "username", "testuser" },
    { "password", "securepassword" }
};

var request = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/post")
    .WithMethod(HttpMethod.Post)
    .WithFormUrlEncodedContent(formData);

Multipart Form-Data (File Upload)

var multipartRequest = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/post")
    .WithMethod(HttpMethod.Post)
    .WithFormMultiPartContent(builder => builder
        .AddTextContent("description", "Test file upload")
        .AddFileContent("file", File.ReadAllBytes("test.txt"), "test.txt"));

Handling JSON Responses

var request = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/json")
    .WithMethod(HttpMethod.Get);

var response = await request.SendObjectAsync<MyResponseModel>();

if (response.IsSuccess)
{
    Console.WriteLine(response.Data.SomeProperty);
}

Configuring Headers

var request = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/get")
    .WithMethod(HttpMethod.Get)
    .WithHeader("Authorization", "Bearer YOUR_TOKEN")
    .WithHeader("Custom-Header", "HeaderValue");

Setting Custom Timeout

var request = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/get")
    .WithMethod(HttpMethod.Get)
    .WithTimeout(TimeSpan.FromSeconds(10));

Using a Custom HttpClient

var httpClient = new HttpClient();
var request = new HttpRequestBuilder(httpClient)
    .WithRequestUri("https://httpbin.org/get")
    .WithMethod(HttpMethod.Get);

Handling Multipart Requests

var request = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/post")
    .WithMethod(HttpMethod.Post)
    .WithFormMultiPartContent(builder => builder
        .AddTextContent("key", "value")
        .AddFileContent("file", File.ReadAllBytes("image.png"), "image.png"));

DHttpClient - Send Functionalities Overview

Sending Requests in DHttpClient

DHttpClient provides multiple methods for sending HTTP requests and handling responses in a structured way using the Result<T> wrapper.


SendAsync()

Description:

Sends the HTTP request asynchronously and returns the raw HttpResponseMessage wrapped in a Result<HttpResponseMessage>.

Usage:

var request = new HttpRequestBuilder()
    .WithRequestUri("https://httpbin.org/get")
    .WithMethod(HttpMethod.Get);

var result = await request.SendAsync();

if (result.IsSuccess)
{
    Console.WriteLine($"Response Status: {result.Data.StatusCode}");
}
else
{
    Console.WriteLine($"Error: {result.ErrorMessage}");
}

SendStringAsync()

Description:

Sends the HTTP request and returns the response body as a string, wrapped in a Result<string>.

Usage:

var result = await request.SendStringAsync();

if (result.IsSuccess)
{
    Console.WriteLine(result.Data); // The raw response content
}
else
{
    Console.WriteLine($"Error: {result.ErrorMessage}");
}

SendObjectAsync<T>()

Description:

Sends the HTTP request and deserializes the JSON response to an object of type T, wrapped in a Result<T>.

Usage:

public class ApiResponse
{
    public string Message { get; set; }
}

var result = await request.SendObjectAsync<ApiResponse>();

if (result.IsSuccess)
{
    Console.WriteLine(result.Data.Message); // Access the deserialized object
}
else
{
    Console.WriteLine($"Error: {result.ErrorMessage}");
}

SendStreamAsync()

Description:

Sends the request and returns the response content as a Stream, wrapped in a Result<Stream>. Useful for downloading large files.

Usage:

var result = await request.SendStreamAsync();

if (result.IsSuccess)
{
    using var fileStream = File.Create("downloadedFile.txt");
    await result.Data.CopyToAsync(fileStream);
    Console.WriteLine("File downloaded successfully.");
}
else
{
    Console.WriteLine($"Error: {result.ErrorMessage}");
}

SendBytesAsync()

Description:

Sends the request and returns the response content as a byte[], wrapped in a Result<byte[]>. Useful for handling binary data.

Usage:

var result = await request.SendBytesAsync();

if (result.IsSuccess)
{
    File.WriteAllBytes("image.png", result.Data);
    Console.WriteLine("Image downloaded successfully.");
}
else
{
    Console.WriteLine($"Error: {result.ErrorMessage}");
}

Error Handling

Each method returns a Result<T> that includes:

  • IsSuccess: Indicates if the request was successful.
  • Data: The response content in the respective format (HttpResponseMessage, string, T, Stream, byte[]).
  • ErrorMessage: Contains error details if the request fails.
  • StatusCode: The HTTP status code of the response (if available).

Example:

var response = await request.SendStringAsync();

if (!response.IsSuccess)
{
    Console.WriteLine($"Error: {response.ErrorMessage}");
}

These send functionalities provide structured error handling and typed responses, making HTTP operations in .NET applications more robust and reliable. 🚀


License

This project is licensed under the MIT License.


Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have improvements.


Contact

For questions or issues, open an issue on GitHub.

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 is compatible.  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 is compatible.  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.2 78 3/15/2025
1.0.1 104 3/2/2025
1.0.0 91 3/1/2025

Release Notes – DHttpClient Optimizations v1.0.1

Enhancements & Optimizations:

Optimized Reflection in ObjectExtensions:

Refactored the ToKeyValue extension method to use a single-pass iteration over public properties. This change reduces unnecessary overhead when converting objects to key/value pairs.
Enhanced MultiPartContentBuilder:

Added a new overload (AddFileContentFromPath) that accepts a file path. This simplifies adding file content by reading bytes directly from a specified file, making file uploads more convenient.
Streamlined HttpRequestBuilder:

Removed retry and logging functionality to simplify the builder’s core implementation. This makes the code leaner and easier to maintain while still offering a full-featured fluent API for constructing and sending HTTP requests.
ASP.NET Core Integration:

Introduced extension methods to facilitate registration with the ASP.NET Core dependency injection container. This integration leverages IHttpClientFactory for creating underlying HttpClient instances, ensuring that your DHttpClient can be easily injected into controllers or services.
Additional Notes:

These optimizations aim to improve performance, reduce complexity, and enhance developer experience without altering the core functionality of the HTTP client.
The changes remain backward-compatible with existing usage patterns while providing a more efficient implementation.
Future enhancements may include additional content type support and more advanced error handling mechanisms as needed.