From 178e1801e98bcf4a32d1fff225242fe6b51ba0b8 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 15 May 2022 16:17:45 +0100 Subject: [PATCH] Add `axum_extra::extract::Form` (#1031) * Add `axum_extra::extra::Form` * update tokio-util ban --- axum-extra/CHANGELOG.md | 2 +- axum-extra/Cargo.toml | 2 + axum-extra/src/extract/form.rs | 136 +++++++++++++++++++++++++++++++++ axum-extra/src/extract/mod.rs | 7 ++ axum/CHANGELOG.md | 1 + axum/src/extract/form.rs | 4 +- axum/src/extract/mod.rs | 11 +-- axum/src/extract/query.rs | 2 +- axum/src/extract/rejection.rs | 5 +- deny.toml | 4 +- 10 files changed, 161 insertions(+), 13 deletions(-) create mode 100644 axum-extra/src/extract/form.rs 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 `