Making HTTP GET request with custom header

I’m trying to make an HTTP GET request to an endpoint which requires a custom header.

The curl request for it looks like:

curl -X 'GET' \
  'http://example.com/api/v1/get/all' \
  -H 'accept: application/json' \
  -H 'X-API-Key: some-token'

in which X-API-Key is the custom header.

The request variable that I’ve been working on looks like:

    ...
    ...
    var request = try client.open(
        .GET,
        "http://example.com/api/v1/get/all",
        .{
            .server_header_buffer = &buffer,
            .headers = .{
                .accept_encoding = .{ .override = "application/json" },
                //.authorization = .{ .override = "X-API-Key: " ++ some_token },  // doesn't work
            },
            .extra_headers = .{
                .name = "X-API-Key",
                .value = some_token,
            },
        },
    );

    ...
    ...

but it (and a few variations of it) produces compile errors.

Whats the syntax to pass a custom header while making an HTTP request ?

    ...
    ...
    const custom_headers = &[_]std.http.Header{
        .{ .name = "Accept", .value = "application/json" },
        .{ .name = "X-API-Key", .value = some_token },
    };

    var request = try client.open(
        .GET,
        "http://example.com/api/v1/get/all",
        .{
            .server_header_buffer = &buffer,
            .extra_headers = custom_headers,
        },
    );
1 Like