Testing multipart/form-data fails with cryptic error message

I'm trying to write a test for a multipart/form-data endpoint in axum
But the test fails with a cryptic message, I don't understand and can't find any hints at all.

thread 'tests::post_multipart' panicked at 'called Result::unwrap() on an Err value: MultipartError { source: failed to read headers: invalid header name }', src/main.rs:22:58
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

See the full code simplified in a sample in this Gist:

You are putting text1 and "test2" into a place in the HTTP stream where a header is expected. The header and body contents must be separated by at least one blank line. For instance, this works:

        write!(data, "Content-Disposition: form-data; name=\"foo\";\r\n")?;
        write!(data, "\r\n")?;
        write!(data, "text1\r\n")?;

Also, I suggest you to use Reqwest to create your requests rather than the low-level http Request::Builder.

1 Like

Thanks a lot, this was exactly the issue. Based on the error message I was not able to see that.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.