#multipart #derive #actix-web #actix #macro

awmpde

awmpde used for deriving multipart request for actix web 3

9 releases (4 breaking)

0.6.0 Jan 22, 2021
0.5.2 Dec 15, 2020
0.4.1 Dec 14, 2020
0.2.0 Nov 3, 2020
0.1.1 Oct 24, 2020

#136 in HTTP server

43 downloads per month

MIT/Apache

47KB
749 lines

awmpde

Docs Crates.io

A convenience library for working with multipart/form-data in actix-web 3.x.

This library uses actix-multipart internally, and is not a replacement for actix-multipart.

Usage

This crate supports actix-web of versions 3.x only.

awmpde = "0.4.0"

Example

use actix_web::{web, App, post, Error, HttpResponse, HttpServer};
use serde::Deserialize;

#[derive(Deserialize, Debug)]
struct Description {
    genre: String,
    author: String,
    year: i64,
}

#[derive(FromActixMultipart)]
struct Book {
    file: awmpde::File<Vec<u8>>,
    #[serde_json]
    description: Description,
}

#[post("/put_book")]
async fn put_book(book: awmpde::Multipart<Book>) -> Result<HttpResponse, Error> {
    let Book {
        file: awmpde::File { name, inner, ..},
        description
    } = book.into_inner()?;
    std::fs::write(std::path::Path::from("books").join(name), &*inner)?;
    let body = format!("Wrote book with description {:?}", desription);
    Ok(HttpResponse::Ok().body(body))
}

#[post("/update_description")]
#[awmpde::form_or_multipart_unwrap]
async fn update_description(
    awmpde::FormOrMultipart(description): awmpde::FormOrMultipart<Description>,
) -> HttpResponse {
    let body = format!("Got description {:?}", desription);
    HttpResponse::Ok().body(body)
}

#[actix_rt::main]
async fn main() -> Result<(), std::io::Error> {
    actix_web::HttpServer::new(move || {
        actix_web::App::new()
            .route(put_book)
            .route(uupdate_description)
    })
    .bind("0.0.0.0:3000")?
    .run()
    .await
}

Current version: 0.2.0

License: MIT

Dependencies

~27MB
~581K SLoC

 `