#actix-web #msgpack #web #protocols #actix

actix-msgpack

Msgpack payload extractor for Actix Web

7 releases

0.1.1 Dec 24, 2023
0.1.0 Dec 23, 2023
0.0.5 Dec 23, 2023
0.0.4 Sep 16, 2023

#1054 in Encoding

26 downloads per month

MIT license

20KB
433 lines

actix-msgpack

Msgpack payload extractor for Actix Web.

Installation

cargo add actix-msgpack

Documentation

Example

use actix_msgpack::MsgPack;
use actix_web::{post, App, HttpResponse, HttpServer, Responder};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Data {
    payload: String,
}
#[post("/")]
async fn index(data: MsgPack<Data>) -> impl Responder {
    println!("payload: {}", data.payload);
    HttpResponse::Ok().finish()
}

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

You can set custom limit (default is 256kb):

use actix_msgpack::MsgPackConfig;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        let mut msgpack_config = MsgPackConfig::default();
        msgpack_config.limit(1024); // 1kb

        App::new().app_data(msgpack_config).service(index)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

You can use responder:

use actix_msgpack::MsgPackResponseBuilder;

#[derive(Serialize)]
struct Data {
    payload: bool,
}

#[post("/")]
async fn index() -> HttpResponse {
    let payload = Data { payload: true };
    HttpResponse::Ok().msgpack(payload)
}

License

This project is licensed under of MIT license (LICENSE or https://opensource.org/licenses/MIT)

Dependencies

~18–31MB
~537K SLoC