Struct nickel::Response [] [src]

pub struct Response<'a, D: 'a = (), T: 'static + Any = Fresh> { /* fields omitted */ }

A container for the response

Methods

impl<'a, D> Response<'a, D, Fresh>
[src]

Get a mutable reference to the status.

Get a mutable reference to the Headers.

Modify the response with the provided data.

Examples

extern crate hyper;
#[macro_use] extern crate nickel;

use nickel::{Nickel, HttpRouter, MediaType};
use nickel::status::StatusCode;
use hyper::header::Location;

fn main() {
    let mut server = Nickel::new();
    server.get("/a", middleware! { |_, mut res|
            // set the Status
        res.set(StatusCode::PermanentRedirect)
            // update a Header value
           .set(Location("http://nickel.rs".into()));

        ""
    });

    server.get("/b", middleware! { |_, mut res|
            // setting the content type
        res.set(MediaType::Json);

        "{'foo': 'bar'}"
    });

    // ...
}

Writes a response

Examples

use nickel::{Request, Response, MiddlewareResult};

fn handler<'a, D>(_: &mut Request<D>, res: Response<'a, D>) -> MiddlewareResult<'a, D> {
    res.send("hello world")
}

Writes a file to the output.

Examples

use nickel::{Request, Response, MiddlewareResult};
use std::path::Path;

fn handler<'a, D>(_: &mut Request<D>, res: Response<'a, D>) -> MiddlewareResult<'a, D> {
    let favicon = Path::new("/assets/favicon.ico");
    res.send_file(favicon)
}

Return an error with the appropriate status code for error handlers to provide output for.

Sets the header if not already set.

If the header is not set then f will be called. Renders the given template bound with the given data.

Examples

#[macro_use] extern crate nickel;
extern crate hyper;

use nickel::{Nickel, HttpRouter, MediaType};
use hyper::header::ContentType;

fn main() {
    let mut server = Nickel::new();
    server.get("/", middleware! { |_, mut res|
        res.set(MediaType::Html);
        res.set_header_fallback(|| {
            panic!("Should not get called");
            ContentType(MediaType::Txt.into())
        });

        "<h1>Hello World</h1>"
    });

    // ...
}

Renders the given template bound with the given data.

Examples

use std::collections::HashMap;
use nickel::{Request, Response, MiddlewareResult};

fn handler<'a, D>(_: &mut Request<D>, res: Response<'a, D>) -> MiddlewareResult<'a, D> {
    let mut data = HashMap::new();
    data.insert("name", "user");
    res.render("examples/assets/template.tpl", &data)
}

Pass execution off to another Middleware

When returned from a Middleware, it allows computation to continue in any Middleware queued after the active one.

impl<'a, 'b, D> Response<'a, D, Streaming>
[src]

In the case of an unrecoverable error while a stream is already in progress, there is no standard way to signal to the client that an error has occurred. bail will drop the connection and log an error message.

Flushes all writing of a response to the client.

impl<'a, D, T: 'static + Any> Response<'a, D, T>
[src]

The status of this response.

The headers of this response.

Trait Implementations

impl<'a, 'b, D> Write for Response<'a, D, Streaming>
[src]

Write a buffer into this object, returning how many bytes were written. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Attempts to write an entire buffer into this write. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Creates a "by reference" adaptor for this instance of Write. Read more

impl<'a, D, T: 'static + Any> Extensible for Response<'a, D, T>
[src]

Get a reference to the type's extension storage.

Get a mutable reference to the type's extension storage.

impl<'a, D, T: 'static + Any> Pluggable for Response<'a, D, T>
[src]

Return a copy of the plugin's produced value. Read more

Return a reference to the plugin's produced value. Read more

Return a mutable reference to the plugin's produced value. Read more

Create and evaluate a once-off instance of a plugin.

impl<'a, D> Redirect for Response<'a, D>
[src]

Redirect the response to a given target Read more