Http module

read_some

procedure · 1 overloadSource

Reads the next part of the body into buffer and returns how many bytes it wrote: at least one while there is more, then 0 once the body has ended (and on every call after that). It waits for data when none has arrived. For a body that should not be held in memory:

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

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

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

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

    buffer: [4] u8;
    total := 0;

    while true {
        count, error := read_some(*response, buffer);
        if error.kind != .NONE || count == 0 break;
        total += count;
    }

    assert(total == 5);
}

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

The request's max_response_size does not apply: the caller decides when to stop (and close the response to give up).

read_some :: (
    response: *Client_Response,
    buffer: [] u8
) -> (int, Client_Error)

Reads the next part of the body into buffer and returns how many bytes it wrote: at least one while there is more, then 0 once the body has ended (and on every call after that). It waits for data when none has arrived. For a body that should not be held in memory:

The request's max_response_size does not apply: the caller decides when to stop (and close the response to give up).

Parameters

response: *Client_Response
buffer: [] u8

Returns

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

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

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

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

    buffer: [4] u8;
    total := 0;

    while true {
        count, error := read_some(*response, buffer);
        if error.kind != .NONE || count == 0 break;
        total += count;
    }

    assert(total == 5);
}

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

See also