Manapi Http

Send Data

Methods to send data over the HTTP response

Headers

To work headers use net::http::response

router->GET("/", [] (http::req &req, http::resp &resp) 
    -> manapi::future<> {
    // set header
    resp.header("content-type", "text/plain").unwrap();
    // remove header
    resp.remove_header("content-type");
    // check header existence
    if (resp.contains_header("content-type")) { ... }
    // get non-const reference to headers
    auto headers = resp.headers();
    ...
});

Body

Send body content

Plain Text

To send plain text use net::http::response::text

resp.text("Hello World!").unwrap();

JSON Data

To send JSON data use net::http::response::json

resp.json({ { "ok", true }, { "message": "Hello World!" } }).unwrap();

The result is

{
    "ok": true,
    "message": "Hello World!"
}

Form Data

To send form data use net::http::response::form

manapi::net::formdata_send send;
send.set_file("file", "hello.world");
send.set_text("text", "Hello ");
send.set_text("text", "World!");
resp.form(std::move(send)).unwrap();

The result is

file=hello.world
text=Hello 
text=World!

net::formdata_send supports erase and contains methods.

Custom Sync Callback

Use net::http::response::callback_sync to send binary data in synchronous mode

resp.callback_sync([sz = (size_t)100](char *buf, std::size_t s, bool &f) mutable -> ssize_t {
    auto copy = std::min(sz, s);
    ::memset(buf, '1', copy);
    sz -= copy;
    if (!sz) f = true;
    return (ssize_t)copy;
}).unwrap();

Custom Async Callback

Use net::http::response::callback_async to send binary data in asynchronous mode

auto fz = manapi::fs::fstream::create("data.txt", req.cancellation().sub().tm(5000)).unwrap();
resp.header (std::string{manapi::net::http::H_CONTENT_LENGTH}, std::to_string(manapi::unwrap(co_await fz->size()))).unwrap();
resp.callback_async([fz](manapi::slice_view sv, bool &f) mutable -> manapi::future<ssize_t> {
    auto res = co_await fz->fread(sv);
    if (res != sv.size()) f = true;
    co_return res;
}).unwrap();

Stream Callback

Use net::http::response::callback_stream to send binary data in stream mode

auto res = manapi::unwrap(co_await manapi::net::fetch2::fetch("https://127.0.0.1:8000", 
    {}, req.cancellation().sub().tm(5000)));
 
resp.callback_stream([res](manapi::net::http::response::resp_stream_cb cb) mutable 
        -> manapi::future<> {
    manapi::unwrap(co_await res->callback_async([&cb] (manapi::slice_view sv, bool fin) 
            -> manapi::future<ssize_t> {
        // writes |sv| totally
        return cb (sv, fin);
    }));
}).unwrap();

On this page