I am attempting to build a MongoDB-backed Flask application which serves from the same endpoints:
- A HTML web interface by default
- A JSON response if
Content-Type == application/json
The idea is that both a user consuming my application with a browser and a service consuming my API programatically can both hit http://myapp.com/users/12345 The former is served a HTML response and the latter is served a JSON response.
As I understand this is in keeping with 'pure' REST, in contrast to the tradition of serving the API from a separate path such as http://myapp.com/api/users/12345.
There is no discussion of views in the Eve docs, other than to say that results are served as JSON by default and XML if requested.
Is there any clean way to override this behaviour such that:
- The standard Eve JSON response is served if
Content-Type == application/json
- Otherwise, a view applies a template to the data returned by Eve to generate a HTML response?
This seems like it would be an elegant means of creating an application which is both RESTful and DRY.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…