Skip to content

Instantly share code, notes, and snippets.

@rduplain
Created April 24, 2012 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rduplain/2480050 to your computer and use it in GitHub Desktop.
Save rduplain/2480050 to your computer and use it in GitHub Desktop.
Proof a new jsonify API for Flask, which provides a clean JSONP default implementation.
import json
from flask import Flask, abort, current_app, request
def jsonify(*args, **kwargs):
callback = None
callback_func = '_callback_func'
callback_arg = '_callback_arg'
if callback_func in kwargs and callback_arg in kwargs:
raise ValueError('Provide %s or %s, not both.' %
(callback_func, callback_arg))
if callback_func in kwargs:
callback = kwargs.pop(callback_func)
if callback_arg in kwargs:
callback = request.args.get(kwargs.pop(callback_arg))
if callback is None:
abort(400)
json_str = json.dumps(dict(*args, **kwargs),
indent=None if request.is_xhr else 2)
if callback is not None:
content = '%s(%s)' % (str(callback), json_str)
mimetype = 'application/javascript'
else:
content = json_str
mimetype = 'application/json'
return current_app.response_class(content, mimetype=mimetype)
app = Flask(__name__)
@app.route('/jsonify_only/')
def jsonify_only():
return jsonify(one=1, two=2, three=3)
@app.route('/jsonify_with_func/')
def jsonify_with_func():
return jsonify(one=1, two=2, three=3, _callback_func='callback')
@app.route('/jsonify_with_arg/')
def jsonify_with_arg():
# Aborts 400 if _callback_arg not given in request args.
return jsonify(one=1, two=2, three=3, _callback_arg='callback')
if __name__ == '__main__':
app.run(debug=True)
jsonify:
curl http://localhost:5000/jsonify_only/
{
"one": 1,
"three": 3,
"two": 2
}
jsonify with static callback function:
curl http://localhost:5000/jsonify_with_func/
callback({
"one": 1,
"three": 3,
"two": 2
})
jsonify with callback given as argument:
curl http://localhost:5000/jsonify_with_arg/?callback=wrap
wrap({
"one": 1,
"three": 3,
"two": 2
})
jsonify with callback given as argument, but no argument (400):
curl http://localhost:5000/jsonify_with_arg/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
echo
echo jsonify:
echo curl http://localhost:5000/jsonify_only/
echo
curl http://localhost:5000/jsonify_only/
echo
echo
echo
echo jsonify with static callback function:
echo curl http://localhost:5000/jsonify_with_func/
echo
curl http://localhost:5000/jsonify_with_func/
echo
echo
echo
echo jsonify with callback given as argument:
echo curl http://localhost:5000/jsonify_with_arg/?callback=wrap
echo
curl http://localhost:5000/jsonify_with_arg/?callback=wrap
echo
echo
echo
echo jsonify with callback given as argument, but no argument "(400)":
echo curl http://localhost:5000/jsonify_with_arg/
echo
curl http://localhost:5000/jsonify_with_arg/
echo
echo
@dalanmiller
Copy link

@rduplain, was this ever implemented into Flask? I checked the Flask repo but didn't see anything about accepting callbacks for jsonify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment