Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (72 sloc) 2.56 KB
//! An example of decoding requests from an HTML form element
extern crate futures;
extern crate gotham;
extern crate hyper;
extern crate mime;
extern crate url;
use futures::{future, Future, Stream};
use hyper::{Body, StatusCode};
use url::form_urlencoded;
use gotham::handler::{HandlerFuture, IntoHandlerError};
use gotham::helpers::http::response::create_response;
use gotham::router::builder::{build_simple_router, DefineSingleRoute, DrawRoutes};
use gotham::router::Router;
use gotham::state::{FromState, State};
/// Extracts the elements of the POST request and responds with the form keys and values
fn form_handler(mut state: State) -> Box<HandlerFuture> {
let f = Body::take_from(&mut state)
.then(|full_body| match full_body {
Ok(valid_body) => {
let body_content = valid_body.into_bytes();
// Perform decoding on request body
let form_data = form_urlencoded::parse(&body_content).into_owned();
// Add form keys and values to response body
let mut res_body = String::new();
for (key, value) in form_data {
let res_body_line = format!("{}: {}\n", key, value);
let res = create_response(&state, StatusCode::OK, mime::TEXT_PLAIN, res_body);
future::ok((state, res))
Err(e) => future::err((state, e.into_handler_error())),
/// Create a `Router`
fn router() -> Router {
build_simple_router(|route| {"/").to(form_handler);
/// Start a server and use a `Router` to dispatch requests
pub fn main() {
let addr = "";
println!("Listening for requests at http://{}", addr);
gotham::start(addr, router())
mod tests {
use super::*;
use gotham::test::TestServer;
fn form_request() {
let test_server = TestServer::new(router()).unwrap();
let response = test_server
"name=Bob&address=123+Jersey Ave.&message=Hello world%21",
assert_eq!(response.status(), StatusCode::OK);
let body = response.read_body().unwrap();
"name: Bob\naddress: 123 Jersey Ave.\nmessage: Hello world!\n".as_bytes()
You can’t perform that action at this time.