Http module

post

procedure · 4 overloadsSource

Adds a POST route; the template, handler and max_body work as for get. A handler reads the body through Json(T), Form(T) or a *Body parameter.

Example
Item :: struct {
    name: string;
}

add :: (item: Json(Item)) -> string {
    return tprint("added %", item.temporary.name);
}

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

    post(*app, "/items", add);

    api := test_client(*app);
    response, _ := post(api, "/items", Item.{ name = "pen" }); // a struct is sent as JSON
    defer close(*response);

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

    assert(response.status == 200 && text == "added pen");
}

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

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

Adds a POST route; the template, handler and max_body work as for get. A handler reads the body through Json(T), Form(T) or a *Body parameter.

Parameters

app: *App
$template: string
$handler: $H
max_body := 0
$loc := #caller_location
Example
Item :: struct {
    name: string;
}

add :: (item: Json(Item)) -> string {
    return tprint("added %", item.temporary.name);
}

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

    post(*app, "/items", add);

    api := test_client(*app);
    response, _ := post(api, "/items", Item.{ name = "pen" }); // a struct is sent as JSON
    defer close(*response);

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

    assert(response.status == 200 && text == "added pen");
}

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

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

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

Parameters

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

POST with value as a JSON body (Content-Type: application/json). post(client, url) sends no body. A request with any other body is a send. POST with value as a JSON body (Content-Type: application/json); a struct, array or string is encoded with Extensions/Json.

Parameters

client: *Client
url: string
value: $V
headers: [] Header_Field

Returns

post :: (
    client: *Client,
    url: string,
    headers: [] Header_Field = .[]
) -> (Client_Response, Client_Error)

POST with no body.

Parameters

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

Returns

See also