diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index 0175d6f6..6784d878 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning]. # Unreleased -- None. +- **added:** Add `extract::Form` which supports multi-value items # 0.3.1 (10. May, 2022) diff --git a/axum-extra/Cargo.toml b/axum-extra/Cargo.toml index b5c8c1a6..5a8d03c9 100644 --- a/axum-extra/Cargo.toml +++ b/axum-extra/Cargo.toml @@ -17,6 +17,7 @@ typed-routing = ["axum-macros", "serde", "percent-encoding"] cookie = ["cookie-lib"] cookie-signed = ["cookie", "cookie-lib/signed"] cookie-private = ["cookie", "cookie-lib/private"] +form = ["serde", "serde_html_form"] spa = ["tower-http/fs"] [dependencies] @@ -36,6 +37,7 @@ serde = { version = "1.0", optional = true } serde_json = { version = "1.0.71", optional = true } percent-encoding = { version = "2.1", optional = true } cookie-lib = { package = "cookie", version = "0.16", features = ["percent-encode"], optional = true } +serde_html_form = { version = "0.1", optional = true } [dev-dependencies] axum = { path = "../axum", version = "0.5", features = ["headers"] } diff --git a/axum-extra/src/extract/form.rs b/axum-extra/src/extract/form.rs new file mode 100644 index 00000000..bcd8c180 --- /dev/null +++ b/axum-extra/src/extract/form.rs @@ -0,0 +1,136 @@ +use axum::{ + async_trait, + body::HttpBody, + extract::{ + rejection::{FailedToDeserializeQueryString, FormRejection, InvalidFormContentType}, + FromRequest, RequestParts, + }, + BoxError, +}; +use bytes::Bytes; +use http::{header, Method}; +use serde::de::DeserializeOwned; +use std::ops::Deref; + +/// Extractor that deserializes `application/x-www-form-urlencoded` requests +/// into some type. +/// +/// `T` is expected to implement [`serde::Deserialize`]. +/// +/// # Differences from `axum::extract::Form` +/// +/// This extractor uses [`serde_html_form`] under-the-hood which supports multi-value items. These +/// are sent by multiple `` attributes of the same name (e.g. checkboxes) and `