pub struct Form<T>(pub T);
Expand description
URL encoded payload extractor and responder.
Form
has two uses: URL encoded responses, and extracting typed data from URL request payloads.
§Extractor
To extract typed data from a request body, the inner type T
must implement the
DeserializeOwned
trait.
Use FormConfig
to configure extraction options.
§Examples
use actix_web::{post, web};
use serde::Deserialize;
#[derive(Deserialize)]
struct Info {
name: String,
}
// This handler is only called if:
// - request headers declare the content type as `application/x-www-form-urlencoded`
// - request payload deserializes into an `Info` struct from the URL encoded format
#[post("/")]
async fn index(web::Form(form): web::Form<Info>) -> String {
format!("Welcome {}!", form.name)
}
§Responder
The Form
type also allows you to create URL encoded responses by returning a value of type
Form<T>
where T
is the type to be URL encoded, as long as T
implements Serialize
.
§Examples
use actix_web::{get, web};
use serde::Serialize;
#[derive(Serialize)]
struct SomeForm {
name: String,
age: u8
}
// Response will have:
// - status: 200 OK
// - header: `Content-Type: application/x-www-form-urlencoded`
// - body: `name=actix&age=123`
#[get("/")]
async fn index() -> web::Form<SomeForm> {
web::Form(SomeForm {
name: "actix".to_owned(),
age: 123
})
}
§Panics
URL encoded forms consist of unordered key=value
pairs, therefore they cannot be decoded into
any type which depends upon data ordering (eg. tuples). Trying to do so will result in a panic.
Tuple Fields§
§0: T
Implementations§
source§impl<T> Form<T>
impl<T> Form<T>
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwrap into inner T
value.
Trait Implementations§
source§impl<T> FromRequest for Form<T>where
T: DeserializeOwned + 'static,
impl<T> FromRequest for Form<T>where
T: DeserializeOwned + 'static,
See here for example of usage as an extractor.
source§fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future
Self
from request parts asynchronously.source§impl<T: Ord> Ord for Form<T>
impl<T: Ord> Ord for Form<T>
source§impl<T: PartialOrd> PartialOrd for Form<T>
impl<T: PartialOrd> PartialOrd for Form<T>
source§impl<T: Serialize> Responder for Form<T>
impl<T: Serialize> Responder for Form<T>
See here for example of usage as a handler return type.
type Body = EitherBody<String>
source§fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body>
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body>
HttpResponse
.impl<T: Eq> Eq for Form<T>
impl<T> StructuralPartialEq for Form<T>
Auto Trait Implementations§
impl<T> Freeze for Form<T>where
T: Freeze,
impl<T> RefUnwindSafe for Form<T>where
T: RefUnwindSafe,
impl<T> Send for Form<T>where
T: Send,
impl<T> Sync for Form<T>where
T: Sync,
impl<T> Unpin for Form<T>where
T: Unpin,
impl<T> UnwindSafe for Form<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.