Skip to content

Conversation

@cllns
Copy link
Member

@cllns cllns commented Jul 25, 2024

These are commonly used for web apps so we should have first-class support for them.

Related: #439

Also, I made Hanami::Action::Mime::TYPES into @api public (from private). The values are already used in apps so it's effectively already public API, so we might as well show them in the API docs. We already link to the constant in the guides, so in the future that could be a link to API docs (once those are working properly).

@cllns cllns requested a review from a team July 25, 2024 18:07
People will use these values in their app, so it's already
effectively in the public API, might as well include it in
the API docs.
@timriley timriley moved this to Todo in Hanami 2.3 Sep 9, 2025
@afomera
Copy link
Member

afomera commented Oct 3, 2025

I tried this branch out with my application, and was able to resolve an issue I had where my Hanami apps had to define the form format for basic form submissions. 🙌

@timriley
Copy link
Member

This is an unequivocally good first step towards improving this stuff. Thanks @cllns! Bringing this in as part of our improvements to format handling for 2.3.

@timriley timriley merged commit 6013b81 into main Oct 10, 2025
@github-project-automation github-project-automation bot moved this from Todo to Done in Hanami 2.3 Oct 10, 2025
@timriley timriley deleted the add-form-and-multipart-formats branch October 10, 2025 03:05
@timriley
Copy link
Member

@cllns Now that I think about it, and after putting together #485, where it is clear that users can add formats with multiple associated MIME types, I wonder if it would make more sense to group the "application/x-www-form-urlencoded" and "multipart/form-data" types under the :html format? There all part of the capabilities that are exposed from HTML, after all?

I'm thinking about the scenario of a user doing config.formats.accept :html in one of their actions. In this case, their action would no longer accept form posts with a Content-Type of application/x-www-form-urlencoded. This means that their actions would reject standard form submissions! (As well as form submissions containing files).

To me, "having my actions accept form submissions" feels like a bog basic aspect of "supporting HTML". In this way, I think that users should not have to remember to add :form, :multipart to the list whenever they do accept :html in their actions.

If we rejigged this TYPES constant to allow multiple MIME type codes to correspond to a format (which the user can already do via their manual config.formats.register), then I think suddenly everything would just work.

I want to get this stuff right (as much as possible, at least) before we release beta2, so I think I'm going to have a play in a test app and explore this idea, but I wanted to write it here first in case you had any feedback in the meantime :)

@timriley
Copy link
Member

Update: I'm taking a slightly different approach. I hope to have some code to share in the next day or two.

The approach is based on storing mime types and content types separately for formats, which allows us to do this:

          html: Format.new(
            name: :html,
            mime_type: "text/html",
            content_types: ["application/x-www-form-urlencoded", "multipart/form-data"]
          )

@timriley
Copy link
Member

timriley commented Oct 17, 2025

OK, these entries are now removed as of #485.

To ensure these content types are accepted for HTML formats, we have the following default format registered:

html: Format.new(
  name: :html,
  media_type: "text/html",
  content_types: ["application/x-www-form-urlencoded", "multipart/form-data"]
)

What this means is this: if :html is configured as an accepted format, then these content types will be permitted, and the response's content type is still set to text/html by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants