Http module

get

procedure · 3 overloadsSource

get(*app, "/users/{id}", handler). max_body (all the verbs take it) is the most request body bytes the route reads: 0 uses config.max_body_size, -1 allows any size (for a handler that streams its *Body); a body over it is a 413 from Json, Form, read and read_all. The template and the handler are compile-time arguments: the template is parsed and every extractor of the handler is checked against it while the program compiles, and the code that fills them in is generated for this route. A route that does not fit is an error that points at the call.

The handler returns a value, a Response, or either one with an Http_Error; a HEAD request is answered from the GET route without the body. get(group, ...) adds the route under a group's prefix.

Example
find_user :: (id: Path("id", int)) -> string, Http_Error {
    if id.value != 1 return "", not_found("No user %", id.value);

    return "Ada", .{};
}

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

    get(*app, "/users/{id}", find_user);

    api := test_client(*app);

    found, _ := get(api, "/users/1");
    assert(found.status == 200);
    close(*found);

    missing, _ := get(api, "/users/2");
    assert(missing.status == 404);
    close(*missing);
}

Doesn't run in the browser (skipped on wasm).

get :: (
    app: *App,
    $template: string,
    $handler: $H,
    max_body := 0,
    $loc := #caller_location
)

get(*app, "/users/{id}", handler). max_body (all the verbs take it) is the most request body bytes the route reads: 0 uses config.max_body_size, -1 allows any size (for a handler that streams its *Body); a body over it is a 413 from Json, Form, read and read_all. The template and the handler are compile-time arguments: the template is parsed and every extractor of the handler is checked against it while the program compiles, and the code that fills them in is generated for this route. A route that does not fit is an error that points at the call.

The handler returns a value, a Response, or either one with an Http_Error; a HEAD request is answered from the GET route without the body. get(group, ...) adds the route under a group's prefix.

Parameters

app: *App
$template: string
$handler: $H
max_body := 0
$loc := #caller_location
Example
find_user :: (id: Path("id", int)) -> string, Http_Error {
    if id.value != 1 return "", not_found("No user %", id.value);

    return "Ada", .{};
}

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

    get(*app, "/users/{id}", find_user);

    api := test_client(*app);

    found, _ := get(api, "/users/1");
    assert(found.status == 200);
    close(*found);

    missing, _ := get(api, "/users/2");
    assert(missing.status == 404);
    close(*missing);
}

Doesn't run in the browser (skipped on wasm).

get :: (
    group: *Group($P),
    $template: string,
    $handler: $H,
    max_body := 0,
    $loc := #caller_location
)

The get for a route under a group's prefix.

Parameters

group: *Group($P)
$template: string
$handler: $H
max_body := 0
$loc := #caller_location
get :: (
    client: *Client,
    url: string,
    headers: [] Header_Field = .[]
) -> (Client_Response, Client_Error)

GET with no body.

Parameters

client: *Client
url: string
headers: [] Header_Field

Returns

See also