Skip to content

Custom Headers ​

Send requests with explicit authentication and tracing headers.

Demo Program ​

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();

    // Custom headers
    var response = try client.get("https://httpbun.com/headers", .{
        .headers = .{
            .authorization = "Bearer my-token",
            .xCustom = "my-value",
        },
    });
    defer response.deinit();

    std.debug.print("Status: {d}\n", .{response.status});
    std.debug.print("Body: {s}\n", .{response.body});

Expected Output ​

text
Status: 200
Body: {"headers":{"Authorization":"Bearer my-token","X-Custom":"my-value","Host":"httpbun.com"}}

The status reflects the live endpoint and the echoed body varies with the server's response shape.

Run ​

bash
zig build run-custom-headers

What to Verify ​

  • Response shows the custom headers echoed by server.
  • Authorization and trace headers are transmitted correctly.

Released under the MIT License.