pub struct Form<T>(pub T);Expand description
Form data helper (application/x-www-form-urlencoded)
Can be used to extract url-encoded data from the request body, or send url-encoded data as the response.
§Extract
To extract typed information from request’s body, the type T must
implement the Deserialize trait from serde.
FormConfig allows to configure extraction
process.
§Example
use ntex::web;
#[derive(serde::Deserialize)]
struct FormData {
username: String,
}
/// Extract form data using serde.
///
/// If the content type is not *x-www-form-urlencoded* or the body cannot be
/// deserialized to a `FormData` struct, the extractor fails with
/// `UrlencodedError` and the handler is not called.
async fn index(form: web::types::Form<FormData>) -> String {
format!("Welcome {}!", form.username)
}§Respond
The Form type also allows you to respond with well-formed url-encoded data:
simply return a value of type Form<T> where T is the type to be url-encoded.
The type must implement serde::Serialize;
§Example
use ntex::web;
#[derive(serde::Serialize)]
struct SomeForm {
name: String,
age: u8
}
// Will return a 200 response with header
// `Content-Type: application/x-www-form-urlencoded`
// and body "name=ntex&age=123"
async fn index() -> web::types::Form<SomeForm> {
web::types::Form(SomeForm {
name: "ntex".into(),
age: 123
})
}Tuple Fields§
§0: TImplementations§
Source§impl<T> Form<T>
impl<T> Form<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Deconstruct to an inner value
Trait Implementations§
impl<T: Eq> Eq for Form<T>
Source§impl<St, T> FromRequest<St> for Form<T>where
St: State,
T: DeserializeOwned + 'static,
impl<St, T> FromRequest<St> for Form<T>where
St: State,
T: DeserializeOwned + 'static,
Source§type Error = UrlencodedError
type Error = UrlencodedError
The error returned when extraction fails. Read more
Source§async fn from_request(
_: &St,
req: &HttpRequest,
payload: &mut Payload,
) -> Result<Self, Self::Error>
async fn from_request( _: &St, req: &HttpRequest, payload: &mut Payload, ) -> Result<Self, Self::Error>
Creates the extractor value for this request.
Source§impl<T: Ord> Ord for Form<T>
impl<T: Ord> Ord for Form<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T: PartialOrd> PartialOrd for Form<T>
impl<T: PartialOrd> PartialOrd for Form<T>
Source§impl<St, T: Serialize> Responder<St> for Form<T>
impl<St, T: Serialize> Responder<St> for Form<T>
Source§async fn respond_to(self, st: &St, _: &HttpRequest) -> Response
async fn respond_to(self, st: &St, _: &HttpRequest) -> Response
Builds an HTTP response from this value.
Source§fn with_status(self, status: StatusCode) -> CustomResponder<Self, St>where
Self: Sized,
fn with_status(self, status: StatusCode) -> CustomResponder<Self, St>where
Self: Sized,
Replaces the status code produced by this responder. Read more
Source§fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self, St>where
Self: Sized,
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
fn with_header<K, V>(self, key: K, value: V) -> CustomResponder<Self, St>where
Self: Sized,
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
<HeaderValue as TryFrom<V>>::Error: Into<HttpError>,
Sets a header on the response produced by this responder. Read more
impl<T: PartialEq> 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> UnsafeUnpin for Form<T>where
T: UnsafeUnpin,
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
Mutably borrows from an owned value. Read more
§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
Compare self to
key and return true if they are equal.