Form - accepting POST request
In this example we can see how an application can accept http POST requestts
To see the responses using curl
:
Asking for the main page with a GET request
$ curl http://localhost:3000/
This returns the HTML page.
$ curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
--data "name=Foo&email=foo@bar.com" \
http://localhost:3000/
email='foo@bar.com'
name='Foo'
Missing field
curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
--data "name=Foo" \
http://localhost:3000/
Failed to deserialize form body: missing field `email`
Extra fields are ignored
$ curl -X POST \
-H "Content-Type: application/x-www-form-urlencoded"
--data "name=Foo&email=foo@bar.com&age=42" \
http://localhost:3000/
email='foo@bar.com'
name='Foo'
[package]
name = "example-form"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
axum = { path = "../../axum" }
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.0", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
[dev-dependencies]
http-body-util = "0.1.3"
mime = "0.3.17"
tower = "0.5.2"
//! Run with //! //! ```not_rust //! cargo run -p example-form //! ``` use axum::{extract::Form, response::Html, routing::get, Router}; use serde::Deserialize; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[tokio::main] async fn main() { tracing_subscriber::registry() .with( tracing_subscriber::EnvFilter::try_from_default_env() .unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()), ) .with(tracing_subscriber::fmt::layer()) .init(); // build our application with some routes let app = app(); // run it let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") .await .unwrap(); tracing::debug!("listening on {}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); } fn app() -> Router { Router::new().route("/", get(show_form).post(accept_form)) } async fn show_form() -> Html<&'static str> { Html( r#" <!doctype html> <html> <head></head> <body> <form action="/" method="post"> <label for="name"> Enter your name: <input type="text" name="name"> </label> <label> Enter your email: <input type="text" name="email"> </label> <input type="submit" value="Subscribe!"> </form> </body> </html> "#, ) } #[derive(Deserialize, Debug)] #[allow(dead_code)] struct Input { name: String, email: String, } async fn accept_form(Form(input): Form<Input>) -> Html<String> { dbg!(&input); Html(format!( "email='{}'\nname='{}'\n", &input.email, &input.name )) } #[cfg(test)] mod tests { use super::*; use axum::{ body::Body, http::{self, Request, StatusCode}, }; use http_body_util::BodyExt; use tower::ServiceExt; // for `call`, `oneshot`, and `ready` // for `collect` #[tokio::test] async fn test_get() { let app = app(); let response = app .oneshot(Request::builder().uri("/").body(Body::empty()).unwrap()) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let body = std::str::from_utf8(&body).unwrap(); assert!(body.contains(r#"<input type="submit" value="Subscribe!">"#)); } #[tokio::test] async fn test_post() { let app = app(); let response = app .oneshot( Request::builder() .method(http::Method::POST) .uri("/") .header( http::header::CONTENT_TYPE, mime::APPLICATION_WWW_FORM_URLENCODED.as_ref(), ) .body(Body::from("name=foo&email=bar@axum")) .unwrap(), ) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let body = std::str::from_utf8(&body).unwrap(); assert_eq!(body, "email='bar@axum'\nname='foo'\n"); } }