Struct ajars_actix_web::actix_web::web::Form [−][src]
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
serde::Deserialize
trait.
Use FormConfig
to configure extraction process.
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 is deserialized into a `Info` struct from the URL encoded format
#[post("/")]
async fn index(form: web::Form<Info>) -> String {
format!("Welcome {}!", form.name)
}
Responder
The Form
type also allows you to create URL encoded responses:
simply return a value of type Formserde::Serialize
.
Responses use
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".into(),
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
Unwrap into inner T
value.
Trait Implementations
See here for example of usage as an extractor.
type Config = FormConfig
type Config = FormConfig
Configuration for this extractor.
type Future = FormExtractFut<T>
type Future = FormExtractFut<T>
Future that resolves to a Self.
pub fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Form<T> as FromRequest>::Future
pub fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Form<T> as FromRequest>::Future
Create a Self from request parts asynchronously.
Create a Self from request head asynchronously. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
See here for example of usage as a handler return type.
pub fn respond_to(self, &HttpRequest) -> HttpResponse<AnyBody>ⓘNotable traits for HttpResponse<AnyBody>impl Future for HttpResponse<AnyBody> type Output = Result<Response<AnyBody>, Error>;
pub fn respond_to(self, &HttpRequest) -> HttpResponse<AnyBody>ⓘNotable traits for HttpResponse<AnyBody>impl Future for HttpResponse<AnyBody> type Output = Result<Response<AnyBody>, Error>;
impl Future for HttpResponse<AnyBody> type Output = Result<Response<AnyBody>, Error>;
Convert self to HttpResponse
.
Override a status code for a Responder. Read more
Insert header to the final response. Read more
pub fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
pub fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
S: Serializer,
Serialize this value into the given Serde serializer. Read more