Http module

options

procedureextensionexperimental3 overloadsSource

Adds an OPTIONS route. Register one only to change the default, which answers with an Allow list of the methods the path has. The template, handler and max_body work as for get.

Example
choices :: () -> Response {
    return with_header(status_only(204), "Allow", "GET, POST");
}

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

    options(*app, "/things", choices);

    api := test_client(*app);
    response, _ := options(api, "/things");
    defer close(*response);

    assert(response.status == 204);
    allow, found := find_header(response.headers, "allow");
    assert(found && allow == "GET, POST");
}

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

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

Parameters

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

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

Parameters

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

OPTIONS: asks what the server allows at url; the Allow header of the response lists the methods.

Parameters

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

Returns

Example
hello :: () -> string {
    return "hello";
}

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

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

    api := test_client(*app);

    response, _ := options(api, "/hello");
    defer close(*response);

    allow, found := header(*response, "Allow");
    assert(found && allow.count > 0);
}

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

See also