Http module

expect_json

procedureextensionexperimentalSource

The body decoded as JSON into a T. The status must be status, or 2xx when it is left out, and the Content-Type JSON. On failure the value is empty. The strings in it are allocated with the context allocator; a test may leave them.

Example
Note :: struct {
    title: string;
}

show :: () -> Response {
    return json(Note.{ title = "milk" });
}

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

    get(*app, "/note", show);

    api := test_client(*app);

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

    note := expect_json(response, Note);
    assert(note.title == "milk");
}

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

expect_json :: (
    response: Client_Response,
    $T: Type,
    status := 0,
    loc := #caller_location
) -> T

The body decoded as JSON into a T. The status must be status, or 2xx when it is left out, and the Content-Type JSON. On failure the value is empty. The strings in it are allocated with the context allocator; a test may leave them.

Parameters

response: Client_Response
$T: Type
status := 0
the exact status wanted. 0 accepts any 2xx.
loc := #caller_location

Returns

T
Example
Note :: struct {
    title: string;
}

show :: () -> Response {
    return json(Note.{ title = "milk" });
}

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

    get(*app, "/note", show);

    api := test_client(*app);

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

    note := expect_json(response, Note);
    assert(note.title == "milk");
}

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

See also