-
-
Notifications
You must be signed in to change notification settings - Fork 984
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
Routing and rendering based on media type/content type negotiation #177
Comments
Hey @daenney this is completely doable with chi today :) The approach I'd suggest based on your description is to create a middleware and put it at the very top of your routing tree, where that middleware parses the request "Accept" header and sets on the context the kind of content response type, and the API version number, then you'll need some helper function or various possibilities to use the right |
Give it a shot, perhaps in a small example forking from _examples/rest and let me know where you come to. If you give me a gist or some working example I may be able to help. The only part that needs some consideration is the pattern to select the appropriate renderer, but given that it's an interface, you could compose Renderers like you do middlewares -- as in, for each kind of payload type, which would read the r.Context(), check the version and then call its versioned payload Renderer method. I initially left it as an exercise for the reader :) there are lots of possibilities. |
Sorry for my lack of response on this! I've gotten the middleware part to work, that was fairly easy. I'm mostly stuck with the Renderer, I'm not quite clear how you mean to compose that like the way you could compose middleware? Do you have a (pseudocode) example of what that could look like? |
btw, the Made a reference here go-chi/render#1 |
I'm looking for some advice on how to potentially do routing and rendering based on media type. As some might have seen a number of API's use content type negotiation with things like
content-type: application/vnd.github.v3+json
for example, to define that you want thev3
representation of the resource inJSON
format without encoding that in the URL.In trying to figure out how to do that it in Chi I got a little stuck. It seems that I would need two things which Chi already does (a combination of the versions and render examples) but modified in such a way that it's not centred around version information being encoded in the URL.
So it seems to me I would need two things:
content-type
header and dispatch based on those (like thearticleRouter()
does)application/vnd.github.v3+json
and understand that it then should treat it as if it were aContentTypeJSON
but simply set a slightly different header fromapplication/json
in the response.That second part, how to use and amend render, is where I'm mostly stuck at figuring out how to elegantly do. Does anyone have any pointers for me on how I could proceed with this? Is there also something obvious you feel I've missed?
P.S I'm not looking to get into a debate about REST API versioning. This is an experiment to see how I could achieve API versioning through content type negotiation with Chi.
The text was updated successfully, but these errors were encountered: