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 Accept header negotiation to relevant API endpoints #337

Merged
merged 10 commits into from Dec 11, 2021

Conversation

tsmethurst
Copy link
Contributor

@tsmethurst tsmethurst commented Dec 11, 2021

This PR adds server-driven content negotiation logic to API endpoints. See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation#server-driven_content_negotiation

This is done by adding a NegotiateAccept function in the api package, which can be called by handler functions that return specific content types.

In cases where the requested content-type is not available, the server will reply with code 406 and an error message:

{
  "error": "no format can be offered for requested Accept header(s) [text/html]; this endpoint offers [application/json]"
}

For now, all handler functions that need to do content-negotiation call the NegotiateAccept function manually. In future, we could look at grouping these handler functions and applying gin middleware to router groups, though this will require some fiddling.

This PR also doesn't handle redirects when a requested content-type is not available, for now it just returns the 406. In future we can look at doing redirects for specific endpoints, but this should (imo) be a separate PR.

closes #328

@tsmethurst tsmethurst merged commit e2daf0f into main Dec 11, 2021
@tsmethurst tsmethurst deleted the accept_negotiation branch December 11, 2021 16:50
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.

[chore] Add proper content-negotiation logic throughout the gin router endpoints
1 participant