Actix-web: how to get the raw request body

Created on 10 Oct 2019  路  4Comments  路  Source: actix/actix-web

how to get the raw request body ? please.

image

pub fn get_rate(req: HttpRequest) -> impl Responder {
    println!("{}", req.path());
    println!("{}", req.query_string());

    let bts = web::Bytes::extract(&req).wait().unwrap();
    println!("bytes锛歿:?}", bts);// got nothing

    JSONResult::new("get rate")
}

Most helpful comment

You put the Query struct next to the bytes parameter as a function argument, like this:

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

#[derive(Deserialize)]
pub struct Person {
    age: u8,
    id: u32,
}

fn handler(bytes: Bytes, query: Query<Person>) -> Result<String, HttpResponse> {
    let name = String::from_utf8(bytes.to_vec()).map_err(|_| HttpResponse::BadRequest().finish())?;
    Ok(format!("Hello, {}!\nYou are user #{} and are {} years old.\n", name, query.id, query.age))
}

fn main() {
    HttpServer::new(||{
        App::new().route("/name", post().to(handler))
    }).bind("127.0.0.1:8080")
        .unwrap()
        .run()
        .unwrap();
}

In CURL this gives me:

> curl "127.0.0.1:8080/name?age=18&id=2" -X POST -d "HiruNya"
Hello, HiruNya!
You are user #2 and are 18 years old.

All 4 comments

The only way I've found is to turn the Bytes into a String using String::from_utf8.

Like this:

use actix_web::{App, HttpResponse, HttpServer, web::{Bytes, post}};

fn handler(bytes: Bytes) -> Result<String, HttpResponse> {
    match String::from_utf8(bytes.to_vec()) {
        Ok(text) => Ok(format!("Hello, {}!\n", text)),
        Err(_) => Err(HttpResponse::BadRequest().into())
    }
}

fn main() {
    HttpServer::new(||{
        App::new().route("/name", post().to(handler))
    }).bind("127.0.0.1:8080")
        .unwrap()
        .run()
        .unwrap();
}

Using curl, I get this:

> curl 127.0.0.1:8080/name -X POST -d "HiruNya"
Hello, HiruNya!

The problem with this is that data is copied when to_vec is used, but I don't see any way around it.

but how to get the query string锛宨s there any way to get the query string and rquest body together.
@HiruNya ,tks.

You put the Query struct next to the bytes parameter as a function argument, like this:

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

#[derive(Deserialize)]
pub struct Person {
    age: u8,
    id: u32,
}

fn handler(bytes: Bytes, query: Query<Person>) -> Result<String, HttpResponse> {
    let name = String::from_utf8(bytes.to_vec()).map_err(|_| HttpResponse::BadRequest().finish())?;
    Ok(format!("Hello, {}!\nYou are user #{} and are {} years old.\n", name, query.id, query.age))
}

fn main() {
    HttpServer::new(||{
        App::new().route("/name", post().to(handler))
    }).bind("127.0.0.1:8080")
        .unwrap()
        .run()
        .unwrap();
}

In CURL this gives me:

> curl "127.0.0.1:8080/name?age=18&id=2" -X POST -d "HiruNya"
Hello, HiruNya!
You are user #2 and are 18 years old.

issue tracker is not good place to ask questions. better to use chat

Was this page helpful?
0 / 5 - 0 ratings