Skip to content
CryoCryo home
Stdlibnet::http

request

import std::net::http::request; · source

Types

Request

type struct Request {
    method:  Method;
    path:    String;
    version: String;
    headers: Headers;
    body:    Array<u8>;
    params:  HashMap<String, String>;

    static new(method: Method, path: Str) -> Request;
    param(&this, name: Str) -> Option<String>;
    with_header(mut this, name: Str, value: Str) -> Request;
    with_body(mut this, body: Array<u8>) -> Request;
    set_body(mut &this, body: Array<u8>) -> void;
    static from_request_line(line: Str) -> Result<Request, IoError>;
    encode_into(&this, out: Array<u8>*) -> Result<(), IoError>;
}

Request::new(method, path) builds one; with_header(name, value) and with_body(body) consume and return it so they chain, and set_body(body) mutates in place. param(name) reads a router capture. encode_into(out) writes the complete HTTP/1.1 message — request line, headers, blank line, body.

mut req: Request = Request::new(Method::Post, Str::new("/api/items"))
    .with_header(Str::new("Content-Type"), Str::new("application/json"))
    .with_body(body);

from_request_line parses the first line of an incoming request; the header and body reads that follow it live in conn. The fields are public, so a handler can read req.headers or req.body directly. MAX_HEADER_LINE and MAX_BODY bound what a reader will accept.

Properties

PropertyTypeNotes
methodMethod
pathString
versionString
headersHeaders
bodyArray<u8>
paramsHashMap<String, String>Path parameters captured by the router (/users/:id -> id). Empty for requests dispatched without a Router.

Constructors

static new(method: Method, path: Str) -> Request;

Construct a minimal request. Clients build on top of this with with_header / with_body.

static from_request_line(line: Str) -> Result<Request, IoError>;

Parse a Request-Line (METHOD SP PATH SP VERSION, no CRLF) into a request with empty headers and no body. Malformed input is InvalidData, which a server surfaces as 400.

Methods

param(&this, name: Str) -> Option<String>;

Look up a path parameter captured by the router (e.g., :id in /users/:id). Returns an owned copy of the captured segment The caller is responsible for dropping it. Returns None when the parameter was not captured (no router, or pattern did not include a segment with that name).

with_header(mut this, name: Str, value: Str) -> Request;

Builder: insert a header and return the request so calls chain. Consumes and returns this (the owning fields are moved, not copied): Request::new(m, p).with_header(n, v).with_body(b).

with_body(mut this, body: Array<u8>) -> Request;

Builder: set the body (and Content-Length) and return the request.

set_body(mut &this, body: Array<u8>) -> void;

Replace the body and set Content-Length accordingly.

encode_into(&this, out: Array<u8>*) -> Result<(), IoError>;

Append the whole message — request line, headers, blank line, body — to out, normally the connection's pending() buffer.

Trait implementations

implement trait Drop for struct Request

Constants

const HTTP_VERSION:     Str = Str::new("HTTP/1.1");
const MAX_HEADER_LINE:  u64 = 8192;
const MAX_BODY:         u64 = 16777216;
const MAX_HEADER_COUNT: u64 = 100;