Skip to content
CryoCryo home
Stdlibnet

net::http

Enough HTTP to stand up a handler-based server and make client requests with no external dependencies. HTTP/1.1 with keep-alive lives here; HTTP/2 and WebSocket are the sibling modules net::http2 and net::ws.

public module net::http::method;
public module net::http::status;
public module net::http::headers;
public module net::http::request;
public module net::http::response;
public module net::http::conn;
public module net::http::router;
public module net::http::server;
public module net::http::client;

Everything here builds on the transports in net, so the same code runs over plaintext TCP or TLS. net::http re-exports each of its sub-modules, so import std::net::http; brings the whole layer into scope.

FileWhat it holdsImport
methodMethodimport std::net::http::method;
statusStatusCodeimport std::net::http::status;
headersHeaders, the case-insensitive mapimport std::net::http::headers;
requestRequestimport std::net::http::request;
responseResponseimport std::net::http::response;
connread_request / read_response on BufStreamimport std::net::http::conn;
routerRouter, RouteKindimport std::net::http::router;
serverHttpServerimport std::net::http::server;
clientClient, sendimport std::net::http::client;

A server in one file

import std::net::http;
import std::future;

function hello(req: Request) -> Response {
    return Response::text(StatusCode::ok(), Str::new("hi"));
}

function main() -> i32 {
    mut router: Router = Router::new();
    router.get(Str::new("/"), hello);
    router.get(Str::new("/users/:id"), show_user);

    mut server: HttpServer = HttpServer::with_router(addr, &router);
    mut ex: Executor = Executor::new();
    ex.block_on(server.run());
    return 0;
}

A handler is a plain (Request) -> Response function pointer. The Router picks one by method and path; the HttpServer accepts connections and drives them. run() is async and needs an Executor.