roa-body-0.5.0-rc doesn't have any documentation.
Roa-body
An extension crate for roa.
This module provides a context extension PowerBody
.
Read/write body in a easier way.
The roa_core
provides several methods to read/write body.
use roa_core::{Context, Result};
use futures::AsyncReadExt;
use futures::io::BufReader;
use async_std::fs::File;
async fn get(mut ctx: Context<()>) -> Result {
let mut data = String::new();
ctx.req_mut().body().read_to_string(&mut data).await?;
println!("data: {}", data);
ctx.resp_mut()
.write(File::open("assets/author.txt").await?)
.write_str("I am Roa.")
.write_bytes(b"Hey Roa.".as_ref());
Ok(())
}
These methods are useful, but they do not deal with headers, especially Content-*
headers.
The PowerBody
provides more powerful methods to handle it.
use roa_core::{Context, Result};
use roa_body::{PowerBody, DispositionType::*};
use serde::{Serialize, Deserialize};
use askama::Template;
use async_std::fs::File;
use futures::io::BufReader;
#[derive(Debug, Serialize, Deserialize, Template)]
#[template(path = "user.html")]
struct User {
id: u64,
name: String,
}
async fn get(mut ctx: Context<()>) -> Result {
let mut user: User = ctx.read_json().await?;
user = ctx.read_form().await?;
ctx.write_json(&user)?;
ctx.write_file("assets/welcome.html", Inline).await?;
ctx.write_text("Hello, World!")?;
ctx.write_octet(File::open("assets/author.txt").await?)?;
ctx.render(&user)?;
Ok(())
}