Skip to content

How to use the body from the extractor Request<Body> #1994

Answered by R2-t
R2-t asked this question in Q&A
Discussion options

You must be logged in to vote
pub async fn handler(
    State(db_pool): State<AppState>,
    body: Request<Body>,
) -> Result<impl IntoResponse, ApiError> {
    let my_body_bytes = hyper::body::to_bytes(body.into_body())
        .await
        .map_err(|_| ApiError::InternalServerError)?;

    println!(
        "{}",
        String::from_utf8(my_body_bytes.to_vec()).map_err(|_| ApiError::InternalServerError)?
    );
    Ok(())
}

This is what I wanted... Yes I know that I can use String as an extractor, but I don't know how that magic happens, thus I wanted to implement it myself.

Replies: 1 comment 14 replies

Comment options

You must be logged in to vote
14 replies
@R2-t
Comment options

@R2-t
Comment options

@davidpdrsn
Comment options

@R2-t
Comment options

Answer selected by R2-t
@davidpdrsn
Comment options

@R2-t
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants