Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing HTTP multipart POST using a struct in Rocket [duplicate]

I want to parse a HTTP POST in Rocket using a struct. Upon submitting the form it fails.

I read the body data example and have 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 submit 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 with

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 submit a file hence the multipart/form-data. When trying to find the reason, I used a String in the struct to make it simpler. So first it responds with a Matched: and then no matching routes.

This simpler POST works:

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

I am using the latest nightly Rust with rustup.

What am I doing wrong?

like image 321
kometen Avatar asked Dec 03 '25 08:12

kometen


1 Answers

Rocket does not yet support multipart forms.

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

A possible workaround is given in this answer: How to parse multipart forms using abonander/multipart with Rocket?

like image 143
belst Avatar answered Dec 05 '25 20:12

belst