Sending Requests

Start with a client configured as shown in Getting started. Replace the example endpoints with your API.

Choose a Request Helper

The core request extensions cover common request shapes:

Typed response methods need a registered formatter that can read the response's media type. See Content formats when adding another format.

Send a JSON Payload

The JSON extension packages supply PostAsJsonAsync(), PutAsJsonAsync(), and PatchAsJsonAsync(). The following POST creates a resource on your API and expects a JSON response matching the Resource model from Getting started.

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

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

var created = await client.PostAsJsonAsync<Resource>(
    "https://api.example.com/resources",
    new { name = "New resource" });

For XML payloads, use PostAsXmlAsync(), PutAsXmlAsync(), or PatchAsXmlAsync() from Kampute.HttpClient.Xml. For application-specific media types, use SendObjectAsync() with a custom content formatter.

Read a Raw Response

using Kampute.HttpClient;

using var client = new HttpRestClient();
var text = await client.GetAsStringAsync("https://api.example.com/resource");

Dispose the stream returned by GetAsStreamAsync() when you finish reading it. Avoid reading the response body in an AfterReceivingResponse handler for streaming requests, since this consumes the stream before the caller can read it.

Cancellation and Failures

Request helpers accept a CancellationToken; pass the caller's token through your API wrapper. See Error handling for retrying transient connection failures and handling HTTP errors.

See the core extensions, JSON extensions, and XML extensions for signatures and overloads.