Skip to main content
100%

A Flask decorator that takes a response with JSON content and converts it to a well-formatted JSONP response.

✓ Published0🌍 Public
Aadewes
Last edited Sep 17, 2013
Created on Sep 17, 2013

This example shows a Flask decorator that transforms a standard JSON response into a JSONP response by wrapping the payload in a callback function. The decorator checks for a `callback` parameter in the request arguments and aborts with a 404 if it is missing. It then calls the wrapped view function, converts the result to a response object if it is a plain string, and replaces the response data with `callback(payload)` while setting the MIME type to `application/javascript`. The code uses Flask’s `request.args`, `make_response`, and `abort` APIs, along with `functools.wraps` to preserve function metadata.

AI-generated description

Similar vizzes