Getting Started

Use these examples in a .NET application with asynchronous calling code. The URLs are placeholders: replace them with endpoints you can access, and use models that match their response bodies.

Choose a Package

The base package contains HttpRestClient, request helpers, scopes, retry policies, error handlers, compression content wrappers, the content formatter registry, and XML support. It depends on Kampute.Resilience, which provides the retry strategies. JSON packages are separate so applications only reference the JSON library they use.

PackageUse it for
Kampute.HttpClientCore HTTP client, request helpers, scopes, retry behavior, error handling, and XML APIs.
Kampute.HttpClient.JsonJSON APIs using System.Text.Json.
Kampute.HttpClient.NewtonsoftJsonJSON APIs that require Newtonsoft.Json features or compatibility.

Install

For a JSON API using System.Text.Json, install the extension package. It brings in the core client and Kampute.Resilience as dependencies.

dotnet add package Kampute.HttpClient.Json

For XML or raw response bodies, install Kampute.HttpClient instead. For JSON using Newtonsoft.Json, install Kampute.HttpClient.NewtonsoftJson. See Content formats for registration and serializer settings.

Read a Typed Response

The core client starts with no content formatter. Register one before reading a response as a .NET object. This example expects application/json content such as {"Id":42,"Name":"Example"}.

using Kampute.HttpClient;
using Kampute.HttpClient.Json;

using var client = new HttpRestClient();
client.UseJson();

var resource = await client.GetAsync<Resource>("https://api.example.com/resource");

public sealed class Resource
{
    public int Id { get; set; }
    public string? Name { get; set; }
}

UseJson() registers the formatter for reading responses and writing payloads. The client uses its readable media types to populate Accept when the request does not already set that header.

Next Steps