question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using the same endpoint for serving a Template and a JSON response

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
tiangolocommented, Oct 21, 2022

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. 🤓

Sorry for the long delay! 🙈 I wanted to personally address each issue and they piled up through time, but now I’m checking each one in order.

3reactions
acmpo6oucommented, Dec 6, 2021

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 is application/json or render Jinja2 template if the accept header is text/html.

You can find the code here html_json.py contains code for the decorator and main.py contains some examples on how to use the decorator.

Hope this helps somebody!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found