tokio-rs/axum

Add a native way to create and respond to requests with multipart forms

Opened this issue · 2 comments

  • I have looked for existing issues (including closed) about this

Feature Request

Motivation

Currently, as far as I can tell, Axum does not provide a native way to respond to requests with multipart forms.

An endpoint might return a multipart form for a variety of reasons, but the most common being the need to include multiple pieces of data (a json, a file, plaintext) in a single request.

For example, I would like to send a json and a file in a single response.

Proposal

I am not comfortable with the Axum codebase, so I am unsure of the most idiomatic ways do this, but there are a few options that may work:

Implement IntoResponse for the Multipart extractor, and add methods like add_field() to the existing impls for multipart.

It may also make more sense to create a separate MultipartBuilder and implement IntoResponse for that type, and add builder methods, Reqwest's implementation could be used as a reference.

I don't think there are any significant drawbacks, it will marginally increase the complexity of exposed formdata APIs, but I believe the benefits outweigh the drawbacks.

Alternatives

As far as I can tell, alternatives involve:

  • Having users wrap reqwest's implementation and manually interop
    • This adds a level of complexity, for no real advantages from an interface perspective.
  • Having users entirely create multipart form builders from scratch.

I am willing to attempt to implement this, however I am not confident in my abilities to do so.

Implementing IntoResponse for the Multipart extractor seems extremely hard to impossible. I think a separate type would make more sense. Further, I think such a thing is niche enough that it shouldn't go into the axum crate, but axum-extra could fit.

I'll look into adding a separate type to to axum-extra, although I don't know how far I'll get.

Thanks for the feedback.