Skip to content

Conversation

@lucas42
Copy link

@lucas42 lucas42 commented Oct 23, 2025

Currently, (as far as I understand), axum-codec only works with Accept headers which contain a single mime type, and provide no relative weights. That means only a small subset of the full range of Accept headers supported by the HTTP spec (rfc7231) will work with it.

This PR attempts to support full content negotiation of Accept headers, including relative weights. To do this, it relies heavily on a pre-existing crate, accept-header, rather than adding all the parsing logic here.

I'm very new to rust (started writing it yesterday), so I've probably made this much more verbose than it needs to be. Would appreciate any input from more experienced rustaceans.

@matteopolak
Copy link
Owner

Hi, thank you for the PR! It looks like the accept-header crate is a (very) thin wrapper over mime, which is already used. accept-header also doesn't seem to be used much so I don't think it's a good idea to add it as a dependency.

The relevant code (these few dozen lines) are basically all that's needed,

The mediatype crate looks promising and could also replace mime. If you're interested in taking a shot at implementing it I can help along the way - if not all good, I can do it :)

@lucas42
Copy link
Author

lucas42 commented Nov 8, 2025

Thanks for taking a look!
After raising this PR, I also did some digging into the internals of the accept-header library and came to a similar realisation myself. (I was trying to get parameters in a mimetype to be returned and found that accept-header doesn't handle those, even though they are part of the underlying mime logic).
So I've ended up just using mime directly in my application code now.
When I came to trying to implement a content negotiated CSV output, I just couldn't get axum-codec and the csv crate to work nicely together. And it wasn't just a matter of adding another feature flag for csv: the csv crate handles serialisation row-by-row, but your library expects to serialise the entire document at once.

So, in the end, I've just done a lightweight content negotiation function in my application with custom serialisation logic for each output type, which allows me to switch between row-based serialisation and document-based serialisation.

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.

2 participants