Struct nickel::Response
[−]
[src]
pub struct Response<'a, T: 'static + Any = Fresh> {
// some fields omitted
}A container for the response
Methods
impl<'a> Response<'a, Fresh>
fn from_internal<'c, 'd>(response: HyperResponse<'c, Fresh>, templates: &'c TemplateCache) -> Response<'c, Fresh>
fn status_mut(&mut self) -> &mut StatusCode
Get a mutable reference to the status.
fn headers_mut(&mut self) -> &mut Headers
Get a mutable reference to the Headers.
fn set<T: Modifier<Response<'a>>>(&mut self, attribute: T) -> &mut Response<'a>
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'}" }); // ... }
fn send<T: Responder>(self, data: T) -> MiddlewareResult<'a>
Writes a response
Examples
use nickel::{Request, Response, MiddlewareResult, Halt}; fn handler<'a>(_: &mut Request, res: Response<'a>) -> MiddlewareResult<'a> { res.send("hello world") }
fn send_file(self, path: &Path) -> MiddlewareResult<'a>
Writes a file to the output.
Examples
use nickel::{Request, Response, MiddlewareResult, Halt}; use nickel::status::StatusCode; use std::path::Path; fn handler<'a>(_: &mut Request, mut res: Response<'a>) -> MiddlewareResult<'a> { let favicon = Path::new("/assets/favicon.ico"); res.send_file(favicon) }
fn error<T>(self, status: StatusCode, message: T) -> MiddlewareResult<'a> where T: Into<Cow<'static, str>>
Return an error with the appropriate status code for error handlers to provide output for.
fn set_header_fallback<F, H>(&mut self, f: F) where H: Header + HeaderFormat, F: FnOnce() -> H
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>" }); // ... }
fn render<T, P>(self, path: P, data: &T) -> MiddlewareResult<'a> where T: Encodable, P: AsRef<str> + Into<String>
Renders the given template bound with the given data.
Examples
use std::collections::HashMap; use nickel::{Request, Response, MiddlewareResult, Halt}; fn handler<'a>(_: &mut Request, mut res: Response<'a>) -> MiddlewareResult<'a> { let mut data = HashMap::new(); data.insert("name", "user"); res.render("examples/assets/template.tpl", &data) }
fn start(self) -> Result<Response<'a, Streaming>, NickelError<'a>>
impl<'a, 'b> Response<'a, Streaming>
fn bail<T>(self, message: T) -> MiddlewareResult<'a> where T: Into<Cow<'static, str>>
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.
fn end(self) -> Result<()>
Flushes all writing of a response to the client.
impl<'a, T: 'static + Any> Response<'a, T>
fn status(&self) -> StatusCode
The status of this response.
fn headers(&self) -> &Headers
The headers of this response.