Parsing HTTP multipart POST using structure in Rocket

I want to parse HTTP POST to Rocket using struct. When the form is submitted, it fails.

I read the body data example and got this code.

#[derive(FromForm)]
struct ConvertFile {
    name: String,
    filename: String
}

#[post("/submit", format = "multipart/form-data", data = "<form>")]
fn submit(form: Form<ConvertFile>) {
    println!("form field: {}", form.get().name);
}

      

I am posting using curl:

curl -H "Content-Type: multipart/form-data" -F "name=Claus" -F "filename=claus.jpg" http://localhost:8000/submit

      

and the Rocket console responds

multipart/form-data; boundary=------------------------8495649d6ed34d20:
    => Matched: POST /submit multipart/form-data
    => Warning: Form data does not have form content type.
    => Outcome: Forward
    => Error: No matching routes for POST /submit multipart/form-data; boundary=------------------------8495649d6ed34d2.
    => Warning: Responding with 404 Not Found catcher.
    => Response succeeded.

      

I want to send a file hence multipart/form-data

. While trying to find the reason I used String

in structure to simplify it. So at first it responds Matched:

and then there are no corresponding routes.

This simple POST works:

#[post("/convert", format = "text/plain", data = "<file>")]
fn convert_file(file: String) {
    println!("file: {}", file);
}

      

I am using the latest night rust from rustup.

What am I doing wrong?

+3


source to share


1 answer


Rocket is not yet in shape multipart

.

You can see the tracking issue here: https://github.com/SergioBenitez/Rocket/issues/106



This answer provides a possible workaround: How to parse multipart forms using abonander / multipart with Rocket?

+3


source







All Articles