Http module

Json

structSource

The whole request body as JSON, read before the handler runs (read_json does the same from a *Body). Content-Type: application/json is required (415 otherwise), a body that is not valid JSON is a 400 and one of the wrong shape a 422; both name the JSON path. A body over the route's max_body is a 413. The T lives in temporary storage for the request: copy the strings you keep.

Example
New_User :: struct {
    name: string;
}

User :: struct {
    id: int;
    name: string;
}

create :: (body: Json(New_User)) -> User {
    return .{ id = 1, name = copy_string(body.temporary.name) };
}

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

    post(*app, "/users", create);

    api := test_client(*app);
    response, _ := post(api, "/users", New_User.{ name = "ada" });
    defer close(*response);

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

    assert(response.status == 200 && text.count > 0);
}

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

Fields

temporary: T

See also