Skip to main content

Forms

Handling URL-encoded form data

Actix 4.9+ 5 min read

# Forms

HTML forms typically submit data as application/x-www-form-urlencoded. Actix Web provides the web::Form extractor to handle this data type easily.

#Extracting Form Data

Like JSON and Query extractors, Form extraction relies on Serde.

code_blocksrust
use actix_web::{post, web, HttpResponse};
use serde::Deserialize;

#[derive(Deserialize)]
struct Login {
    username: String,
    password: String,
}

#[post("/login")]
async fn login(form: web::Form<Login>) -> HttpResponse {
    HttpResponse::Ok().body(format!("User {} logged in", form.username))
}

#Form Configuration

Similar to JSON, you can configure the maximum payload size for forms to protect your server.

code_blocksrust
use actix_web::{web, App};

App::new().app_data(web::FormConfig::default().limit(16384))

Was this page helpful?

Reference Implementation

The following code snippet demonstrates the authoritative pattern for this documentation.

src/main.rs
1use actix_web::{post, web, App, HttpServer, Responder, HttpResponse};
2use serde::Deserialize;
3
4#[derive(Deserialize)]
5struct Contact {
6 message: String,
7}
8
9#[post("/contact")]
10async fn contact_us(form: web::Form<Contact>) -> impl Responder {
11 HttpResponse::Ok().body(format!("Received message: {}", form.message))
12}
13
14#[actix_web::main]
15async fn main() -> std::io::Result<()> {
16 HttpServer::new(|| App::new().service(contact_us))
17 .bind(("127.0.0.1", 8080))?
18 .run()
19 .await
20}
Was this page helpful?