Struct actix_web::web::Form

source ·
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§

Unwrap into inner T value.

Trait Implementations§

Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Formats the value using the given formatter. Read more

See here for example of usage as an extractor.

The associated error which can be returned.
Future that resolves to a Self. Read more
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. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

See here for example of usage as a handler return type.

Convert self to HttpResponse.
Wraps responder to allow alteration of its response. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Type of resource’s path returned in resource_path.
Should always be Self
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more