Piotr Płaczek
Piotr Płaczek

Reputation: 610

How to get (binary) payload from actix_web::HttpRequest

I'm writing some web api in Rust. I send Unit8Array from JavaScript with XMLHttpRequest, and I need to read them in server as Bytes.

Declaration of my service method is:

pub fn user_login_bin(mut req: HttpRequest) {
 println!("{:?}", req);
 let mut stream = req.take_payload().take();

 // error label: method not found in `actix_http::payload::Payload<()>`
 let item = stream.poll().unwrap(); 

 println!("{:?}", item);

 HttpResponse::Ok().into()
}

How can i read Vec from actix_web::HttpRequest payload ?

I tried some example code but it doesn't work too:

 req.take_payload()
    // error label: method (fold) not found in `actix_http::payload::Payload<()>`
    .fold(BytesMut::new(), move |mut body, chunk| {
        body.extend_from_slice(&chunk);
        Ok::<_, Error>(body)
    })
    .and_then(|bytes| {
        println!("request body: {:?}", bytes);
    });

Upvotes: 6

Views: 2556

Answers (1)

Piotr Płaczek
Piotr Płaczek

Reputation: 610

Bytes extractor works fine

pub fn user_login_bin(req: HttpRequest, body: Bytes) -> HttpResponse {
   println!("{:?}", req);
   println!("{:?}", body);

   HttpResponse::Ok().into()
}

Upvotes: 5

Related Questions