Plack::Middleware::JSON::ForBrowsers - Plack middleware which turns application/json responses into HTML
version 0.002000
Basic Usage:
use Plack::Builder; builder { enable 'JSON::ForBrowsers'; $app; };
Combined with Plack::Middleware::Debug:
use Plack::Builder; builder { enable 'Debug'; enable 'JSON::ForBrowsers'; $app; };
Custom HTML head and foot:
use Plack::Builder; builder { enable 'JSON::ForBrowsers' => ( html_head => '<pre><code>', html_foot => '</code></pre>', ); mount '/' => $json_app; };
Plack::Middleware::JSON::ForBrowsers turns application/json
responses into HTML that can be displayed in the web browser. This is primarily intended as a development tool, especially for use with Plack::Middleware::Debug.
The middleware checks the request for the X-Requested-With
header - if it does not exist or its value is not XMLHttpRequest
and the Accept
header indicates that HTML is acceptable, it will wrap the JSON from an application/json
response with HTML and adapt the content type accordingly.
This behaviour should not break clients which expect JSON, as they still do get JSON. But when the same URI is requested with a web browser, HTML-wrapped and pretty-printed JSON will be returned, which can be displayed without external programs or special extensions.
Constructor, creates a new instance of the middleware.
This method expects its parameters as a hash or hash reference.
String that will be prefixed to the prettified JSON instead of the default HTML head. If passed, it must be a UTF-8-encoded character string.
String that will be appended to the prettified JSON instead of the default HTML foot. If passed, it must be a UTF-8-encoded character string.
Specialized call
method. Expects the response body to contain a UTF-8 encoded byte string.
Tries to decide if a request is coming from a web browser. Uses the Accept
and X-Requested-With
headers for this decision.
This method expects positional parameters.
1
if it looks like the request came from a browser, 0
otherwise.
Takes a UTF-8 encoded JSON byte string as input and turns it into a UTF-8 encoded HTML byte string, with HTML entity encoded characters to avoid XSS.
This method expects positional parameters.
The JSON byte string.
The JSON wrapped in HTML.
Manfred Stock <mstock@cpan.org>
This software is copyright (c) 2014 by Manfred Stock.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.