Skip to content

API: Request ​

HTTPX represents outgoing requests in two forms: the high-level methods accept an inline .{} options value, while fluent construction uses httpx.client.requestBuilder (payload type httpx.client.requestPayload). Incoming server requests are exposed through httpx.router.Context, not this request type.

Overview ​

Requests feature a unified options model. Method convenience functions (client.get, client.post, etc.) automatically construct and dispatch a canonical request.

zig
const std = @import("std");
const httpx = @import("httpx");

pub fn main() !void {
    var gpa: std.heap.DebugAllocator(.{}) = .init;
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();
    const io = std.Io.Threaded.global_single_threaded.io();

    var client = httpx.Client.init(allocator, io, .{});
    defer client.deinit();

    // Canonical request via fetch
    var resp = try client.fetch("https://httpbun.com/post", .{
        .method = .POST,
        .headers = &.{
            .{ .name = "Accept", .value = "application/json" },
        },
        .json = .{ .id = 42, .name = "Antigravity" },
        .timeoutMs = 5000,
    });
    defer resp.deinit();
}

Request Options Fields ​

Pass these fields inline as the final .{} argument to request methods. The client accepts duck-typed extras such as multipart through anytype opts. Typed values passed as .json = value are serialized by the high-level wrappers. The fluent payload type is httpx.client.requestPayload.

FieldTypeDescription
url[]const u8Request URL (required)
method?MethodExplicit method for generic request/fetch
headers[]const HeaderSlice of request header name/value pairs
query[]const HeaderURL query parameters automatically encoded
body?[]const u8Raw payload bytes
json?[]const u8JSON string body (sets Content-Type)
form?[]const u8Pre-encoded application/x-www-form-urlencoded body
text?[]const u8Plain-text body
contentType?[]const u8Explicit Content-Type override
cookie?[]const u8Value for Cookie header
basicAuth?[]const u8"user:pass" for Basic Authorization
bearerAuth?[]const u8Token for Bearer Authorization
proxy?[]const u8Proxy URL overriding client default
tlsinline .{}Custom TLS configuration for this request
timeoutMs?u64Request deadline in milliseconds
maxResponseSize?usizeMax response body size for this request
followRedirects?boolWhether to automatically follow 3xx redirects
maxRedirects?u8Maximum redirect hops before error
allowLfLineEndingsboolAccept bare LF line endings in the response
httpVersion?HttpVersionProtocol version (.auto, .http10, .http11, .http2, .http3)
http10?boolFast toggle to force HTTP/1.0
http11?boolFast toggle to force HTTP/1.1
http2?boolFast toggle to force HTTP/2
http3?boolFast toggle to force HTTP/3
earlyDatainline .{}Per-request 0-RTT settings
bodySource?httpx.BodySourceStreaming upload source for HTTP/1.1, HTTP/2, and HTTP/3

Released under the MIT License.