Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

would like support for freeform request bodies #44

Closed
jclulow opened this issue Aug 14, 2020 · 0 comments · Fixed by #48
Closed

would like support for freeform request bodies #44

jclulow opened this issue Aug 14, 2020 · 0 comments · Fixed by #48

Comments

@jclulow
Copy link
Collaborator

jclulow commented Aug 14, 2020

I have an API where I am just accepting a freeform string in a POST. I was able to read a capped amount of bytes, thus:

use hyper::body::HttpBody;

async fn read_body(rqctx: &Arc<RequestContext>, max: usize) -> Result<String> {
    let mut req = rqctx.request.lock().await;
    let body = req.body_mut();
    let mut out: Vec<u8> = Vec::new();

    while let Some(x) = body.data().await.transpose()
        .map_err(|e| anyhow!("body read: {}", e))?
    {
        if out.len().saturating_add(x.len()) > max {
            bail!("request size exceeded {} bytes", max);
        }

        out.append(&mut x.to_vec());
    }

    Ok(String::from_utf8(out)?)
}

It'd be neat if there was some way to get either a UTF-8 String or a slice or vector of u8 (I think both will be useful at different times) through an extractor in the way that you can get a specific Deserializable today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant