Struct tiny_http::Request [] [src]

pub struct Request { /* fields omitted */ }

Represents an HTTP request made by a client.

A Request object is what is produced by the server, and is your what your code must analyse and answer.

This object implements the Send trait, therefore you can dispatch your requests to worker threads.


If a client sends multiple requests in a row (without waiting for the response), then you will get multiple Request objects simultaneously. This is called requests pipelining. Tiny-http automatically reorders the responses so that you don't need to worry about the order in which you call respond or into_writer.

This mechanic is disabled if:

Automatic cleanup

If a Request object is destroyed without into_writer or respond being called, an empty response with a 500 status code (internal server error) will automatically be sent back to the client. This means that if your code fails during the handling of a request, this "internal server error" response will automatically be sent during the stack unwinding.


impl Request

Returns true if the request was made through HTTPS.

Returns the method requested by the client (eg. GET, POST, etc.).

Returns the resource requested by the client.

Returns a list of all headers sent by the client.

Returns the HTTP version of the request.

Returns the length of the body in bytes.

Returns None if the length is unknown.

Returns the address of the client that sent this request.

Note that this is gathered from the socket. If you receive the request from a proxy, this function will return the address of the proxy and not the address of the actual user.

Allows to read the body of the request.


let mut request = server.recv().unwrap();

if get_content_type(&request) == "application/json" {
    let mut content = String::new();
    request.as_reader().read_to_string(&mut content).unwrap();
    let json: Json = content.parse().unwrap();

If the client sent a Expect: 100-continue header with the request, calling this function will send back a 100 Continue response.

Turns the Request into a writer.

The writer has a raw access to the stream to the user. This function is useful for things like CGI.

Note that the destruction of the Writer object may trigger some events. For exemple if a client has sent multiple requests and the requests have been processed in parallel, the destruction of a writer will trigger the writing of the next response. Therefore you should always destroy the Writer as soon as possible.

Sends a response to this request.

Trait Implementations

impl Debug for Request

Formats the value using the given formatter.

impl Drop for Request

A method called when the value goes out of scope. Read more