#actix #multipart #form #derive #macro

macro actix-multipart-extract-derive

Better multipart form support for Actix Web

2 releases

0.1.5 Aug 18, 2022
0.1.4 Jul 4, 2022

#1494 in Procedural macros

Download history 46/week @ 2023-01-24 24/week @ 2023-01-31 30/week @ 2023-02-07 40/week @ 2023-02-14 31/week @ 2023-02-21 19/week @ 2023-02-28 33/week @ 2023-03-07 32/week @ 2023-03-14 68/week @ 2023-03-21 71/week @ 2023-03-28 78/week @ 2023-04-04 241/week @ 2023-04-11 90/week @ 2023-04-18 39/week @ 2023-04-25 81/week @ 2023-05-02 49/week @ 2023-05-09

262 downloads per month
Used in actix-multipart-extract

MIT license

5KB
81 lines

actix-multipart-extract Latest Version

This crate is a Rust library for providing proper multipart support to actix 4.

This is able to parse a multipart request into a struct and validate the request properties. It uses serde for deserialization and a MultipartForm derive.

Installation

Add actix_multipart_extract to your Cargo.toml:

[dependencies]
actix-multipart-extract = "0.5"

Example:

use actix_multipart_extract::{File, Multipart, MultipartForm};
use actix_web::{post, App, HttpResponse, HttpServer, Responder};
use serde::Deserialize;

// File, String, bool, and number types are the only supported types for forms.
// Vec and Option may also be used with one of the 4 types as the type param.
// Some serde attributes will work with forms.
#[derive(Deserialize, MultipartForm, Debug)]
pub struct ExampleForm {
    #[multipart(max_size = 5MB)]
    file_field: File,
    string_field: Vec<String>, // list field
    bool_field: Option<bool>,  // optional field
}

#[post("/example")]
async fn example(example_form: Multipart<ExampleForm>) -> impl Responder {
    // File field
    println!("File size: {}", example_form.file_field.bytes.len());
    println!(
        "File content type: {}",
        example_form.file_field.content_type
    );
    println!("File name: {}", example_form.file_field.name);

    // List of strings field
    println!("List of strings: {:?}", example_form.string_field);

    // Optional bool field
    match example_form.bool_field {
        Some(v) => println!("Has bool field: {}", v),
        None => println!("No bool field"),
    }

    HttpResponse::Ok()
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(move || App::new().service(example))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}

Dependencies

~0.9–1.2MB
~30K SLoC