Esc

Client

structextensionexperimentalSource
  • macOS: Client works on macOS
  • Linux: Client works on Linux
  • Windows: Client works on Windows
  • Wasm: Client works on Wasm

Declaration

Client :: struct {
// Relative request URLs (/users/1) are joined to this: "http://localhost:8080".    base_url: string;
// Sent with every request that does not set the header itself (an Authorization token).    headers: [..] Header_Field;
// Milliseconds for a request that does not set its own timeout.    timeout := 30_000;
// Redirects followed for a request that does not set its own max_redirects (0: none).    max_redirects := 10;
    transport: Transport;
// The TLS settings for https:// URLs; null: trust the system's roots and offer http/1.1
(a config the client makes once for the whole program). A config of your own must have gone
through Tls.tls_config_init and must outlive the client's connections.    tls: *Tls.Tls_Config;}

What requests have in common.

Example
who :: (token: Header("Authorization", string)) -> string {
    return token.value;
}

main :: () {
    app: App;
    defer free_app(*app);

    get(*app, "/who", who);

    api := test_client(*app);
    assert(api.timeout == 10_000 && api.max_redirects == 0); // what `test_client` sets: it does not follow redirects
    set_header(api, "Authorization", "Bearer abc"); // sent with every request

    response, _ := get(api, "/who");
    defer close(*response);

    text, _ := read_text(*response);
    defer free(text);

    assert(text == "Bearer abc");
}

See also