Created
May 28, 2013 21:14
-
-
Save jfinkels/5666195 to your computer and use it in GitHub Desktop.
Example of why mimerender should have some way of exposing HTTP response headers and status code to renderers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python2 | |
from flask import Flask | |
from flask import json | |
from mimerender import FlaskMimeRender | |
app = Flask(__name__) | |
app.debug = True | |
app.testing = True | |
mimerender = FlaskMimeRender() | |
def render_json(*args, **kw): | |
# Here, we would incorporate headers and status code into the keyword | |
# arguments so that the body of the response will have a JSON | |
# representation of them as well. However, they are not available to this | |
# function. | |
# | |
# kw['status_code'] = status_code | |
# kw.update(**headers) | |
# return json.dumps(kw) | |
# | |
return json.dumps(kw) | |
@app.route('/') | |
@mimerender(default='json', json=render_json) | |
def index(): | |
data = dict(foo='bar') | |
status = 201 | |
headers = dict(Location='baz') | |
return data, status, headers | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment