Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multipart/form-data response builders to axum-extra #2654

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

zleyyij
Copy link

@zleyyij zleyyij commented Mar 16, 2024

Closes #2624 (mine)
This pull request provides builders that enable multipart/form-data forms to be returned from handlers.

Motivation

multipart/form-data forms provide a way to include multiple types of data in a single response as key value pairs. You could include an image and a JSON, or any other combination of data.

As an example, this could be used for a pastebin, so that a single request to an axum backend could return metadata about a paste in a json, and the paste itself. The paste wouldn't need to conform to UTF-8, and could be sent separately from the json.

Solution

I added a MultipartForm struct that implements IntoResponse, and implementers can add parts to that form by defining Parts. I did not implement IntoResponse on the Multipart extractor, as advised in the original issue.

One dependency was added if the multipart feature is enabled. fastrand is used to generate boundaries, mirroring Reqwest's implementation.

Thank you for your time :)

@zleyyij
Copy link
Author

zleyyij commented Mar 16, 2024

I may need some help fixing those CI errors, I am unsure how to address them

@zleyyij
Copy link
Author

zleyyij commented May 4, 2024

Ok, I managed to fix all of those CI errors, thank you for your patience

Copy link
Collaborator

@mladedav mladedav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, for the PR I think the functionality looks good. I have not personally worked with multipart on this level so I don't know much of the technical details but I did comment on some of the other stuff.

axum-core/Cargo.toml Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/lib.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
@jplatte jplatte changed the title Closes #2624, added multipart/form-data response builders to axum-extra Add multipart/form-data response builders to axum-extra May 5, 2024
@jplatte jplatte self-requested a review May 5, 2024 19:12
@zleyyij
Copy link
Author

zleyyij commented May 8, 2024

I have commit ready to fix some of those minor issues, I just need to figure out how to handle mime type stuff from a public api level

@zleyyij
Copy link
Author

zleyyij commented Jun 9, 2024

I don't know how to do that, any advice would be appreciated

@zleyyij
Copy link
Author

zleyyij commented Jul 11, 2024

I gave up and just made any mime parsing happen internally, and the associated functions return an error.

I ran as much CI as I could with act locally, hopefully it passes

@zleyyij
Copy link
Author

zleyyij commented Aug 14, 2024

Is there anything more this PR needs?

@jplatte
Copy link
Member

jplatte commented Aug 14, 2024

No, it's just waiting on my review. I've started looking at it now.

axum-extra/src/lib.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
axum-extra/src/multipart_builder.rs Outdated Show resolved Hide resolved
@zleyyij
Copy link
Author

zleyyij commented Aug 15, 2024

Those changes have been applied, thank you for your feedback :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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