Using the same endpoint for serving a Template and a JSON response
See original GitHub issueI would like to serve an HTML page when the accept
header is set to text/html
and serve a JSON object when the accept
header is set to application/json
.
Is that possible? If so, what should I set the response_class
to?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Create A REST API With JSON Server | by Sebastian - Medium
JSON Server is a simple project that helps you to setup a REST API with CRUD ... The JSON structure consists of one...
Read more >Methods from a single view to different endpoints: keep APIs ...
If you want to send a JSON response, you can actually leverage the JSONRenderer ( rest_framework.renderers.JSONRenderer ) which serves the ...
Read more >Best practices for REST API design - Stack Overflow Blog
Accept and respond with JSON. REST APIs should accept JSON for request payload and also send responses to JSON. JSON is the standard...
Read more >Use a mapping template to override an API's request and ...
Standard API Gateway parameter and response code mapping templates allow you to map parameters one-to-one and map a family of integration response status ......
Read more >How to Consume JSON from RESTful Web Service and ...
I will also talk about how to convert the JSON to Java objects using Jackson. ... a RESTful Web Service and consume JSON...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the help here @includeamin and @Kludex ! 🙇
And thanks for the discussion everyone!
@augusto-herrmann interesting, this would mean that routing would depend not only on path and HTTP operation but also on a specific header,
content-type
. I’ve seen others also want other things to be taken into account when routing. E.g. a header defining the version of the app to use, or things like that. I’m still not sure about it, as it’s a lot of extra complexity, to deal with, to code, to maintain, to explain, for users to learn, and I’m not sure the benefits are worth it.But anyway, maybe you could create an independent issue with your specific feature request.
I think the ideas here would solve the original problem from @amjith, right? If that’s the case, then you can close the issue. 🤓
Hi!
I’ve implemented the
@html_or_json
decorator that you can put on your path operation functions to make them return json when the accept header of request isapplication/json
or render Jinja2 template if the accept header istext/html
.You can find the code here
html_json.py
contains code for the decorator andmain.py
contains some examples on how to use the decorator.Hope this helps somebody!