Skip to content

HTTP Client Attributes

Attributes define the shape of your HTTP client — which class is a client, which methods map to endpoints, and how parameters are bound.

Class-Level

[HttpClient] / [HttpClient<TConfig>]

Marks a partial class as an HTTP client. The optional generic parameter binds a configuration type for base address and settings.

Property Type Default Description
BaseAddress string? null Base URL for all requests. Can also be set via configuration.
JsonContext Type? null Optional JsonSerializerContext type for STJ source-generated serialization.
// Without configuration binding
[HttpClient(BaseAddress = "https://api.example.com")]
public partial class SimpleClient;

// With configuration binding
[HttpClient<GitHubApiConfig>]
public partial class GitHubClient;

Configuration binding

When using [HttpClient<TConfig>], the base address and other settings are resolved from the bound configuration provider at runtime.

Method-Level (HTTP Verbs)

Each HTTP verb attribute marks a partial method as an API endpoint.

Attribute HTTP Method
[Get(path)] GET
[Post(path)] POST
[Put(path)] PUT
[Patch(path)] PATCH
[Delete(path)] DELETE

The path parameter is required and supports {param} templates that bind to method parameters:

[Get("/users/{userId}/orders/{orderId}")]
private partial Order GetOrder(int userId, string orderId);

Return types

Methods must return Task<T> where T is the response type (e.g., Task<User>). Use Task<Unit> for endpoints with no response body.

Parameter-Level

Attribute Description Example
[Path] Path parameter — auto-detected from {param} templates int id in /users/{id}
[Query] Query string parameter. Optional Name for override. [Query("per_page")] int perPage
[Header(name)] Adds an HTTP header to the request [Header("X-Api-Version")] string version
[Body] Request body, serialized as JSON [Body] CreateUserRequest request
[Form] Form-urlencoded field. Optional Name for override. [Form("To")] string to

Path parameter auto-detection

Parameters matching {param} placeholders in the path are automatically treated as path parameters — the [Path] attribute is only needed when the parameter name differs from the template placeholder.

[Form] and [Body] are mutually exclusive

A method cannot have both [Form] and [Body] parameters. [Body] sends a JSON payload (application/json), while [Form] sends form-urlencoded fields (application/x-www-form-urlencoded). The analyzer reports DSHTTP05 if both are used on the same method.

Form-Encoded Requests

Use [Form] parameters for APIs that accept application/x-www-form-urlencoded content (e.g., Twilio, OAuth token endpoints):

[Post("/2010-04-01/Accounts/{accountSid}/Messages.json")]
public partial Task<SmsResponse> SendSms(
    string accountSid,
    [Form("To")] string to,
    [Form("From")] string from,
    [Form("Body")] string body);

When the Name is omitted, the parameter name is used as the form field name:

[Post("/oauth/token")]
public partial Task<TokenResponse> GetToken(
    [Form] string grant_type,
    [Form] string client_id,
    [Form] string client_secret);

Authentication

Authentication attributes configure how credentials are attached to requests. See the Authentication page for full details.

Attribute Description
[BearerAuth] Bearer token from configuration
[ApiKeyAuth] API key in a named header
[BasicAuth] Basic username/password credentials