Struct Form
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§
§impl<T> Form<T>
impl<T> Form<T>
pub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwrap into inner T
value.
Trait Implementations§
§impl<T> Apiv2Schema for Form<T>where
T: Apiv2Schema,
impl<T> Apiv2Schema for Form<T>where
T: Apiv2Schema,
§fn name() -> Option<String>
fn name() -> Option<String>
§fn description() -> &'static str
fn description() -> &'static str
§fn raw_schema() -> DefaultSchemaRaw
fn raw_schema() -> DefaultSchemaRaw
§fn schema_with_ref() -> DefaultSchemaRaw
fn schema_with_ref() -> DefaultSchemaRaw
§fn security_scheme() -> Option<SecurityScheme>
fn security_scheme() -> Option<SecurityScheme>
fn header_parameter_schema() -> Vec<Parameter<DefaultSchemaRaw>>
§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.
§fn from_request(
req: &HttpRequest,
payload: &mut Payload,
) -> <Form<T> as FromRequest>::Future
fn from_request( req: &HttpRequest, payload: &mut Payload, ) -> <Form<T> as FromRequest>::Future
Self
from request parts asynchronously.§fn extract(req: &HttpRequest) -> Self::Future
fn extract(req: &HttpRequest) -> Self::Future
Self
from request head asynchronously. Read more§impl<T> OperationModifier for Form<T>where
T: Apiv2Schema,
impl<T> OperationModifier for Form<T>where
T: Apiv2Schema,
§fn update_parameter(
op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>,
)
fn update_parameter( op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>, )
§fn update_definitions(_map: &mut BTreeMap<String, DefaultSchemaRaw>)
fn update_definitions(_map: &mut BTreeMap<String, DefaultSchemaRaw>)
§fn update_response(
_op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>,
)
fn update_response( _op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>, )
§fn update_security(
op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>,
)
fn update_security( op: &mut Operation<Parameter<DefaultSchemaRaw>, Response<DefaultSchemaRaw>>, )
§fn update_security_definitions(map: &mut BTreeMap<String, SecurityScheme>)
fn update_security_definitions(map: &mut BTreeMap<String, SecurityScheme>)
§impl<T> Ord for Form<T>where
T: Ord,
impl<T> Ord for Form<T>where
T: Ord,
§impl<T> PartialOrd for Form<T>where
T: PartialOrd,
impl<T> PartialOrd for Form<T>where
T: PartialOrd,
§impl<T> Responder for Form<T>where
T: Serialize,
impl<T> Responder for Form<T>where
T: Serialize,
See here for example of usage as a handler return type.
type Body = EitherBody<String>
§fn respond_to(
self,
_: &HttpRequest,
) -> HttpResponse<<Form<T> as Responder>::Body>
fn respond_to( self, _: &HttpRequest, ) -> HttpResponse<<Form<T> as Responder>::Body>
HttpResponse
.§impl<T> Serialize for Form<T>where
T: Serialize,
impl<T> Serialize for Form<T>where
T: Serialize,
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl<T> Eq for Form<T>where
T: Eq,
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
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.