NAME
Plack::Middleware::JSON::ForBrowsers - Plack middleware which turns application/json responses into HTML
VERSION
version 0.002000
SYNOPSIS
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;
};
DESCRIPTION
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.
METHODS
new
Constructor, creates a new instance of the middleware.
Parameters
This method expects its parameters as a hash or hash reference.
- html_head
-
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.
- html_foot
-
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.
call
Specialized call
method. Expects the response body to contain a UTF-8 encoded byte string.
looks_like_browser_request
Tries to decide if a request is coming from a web browser. Uses the Accept
and X-Requested-With
headers for this decision.
Parameters
This method expects positional parameters.
- env
-
The PSGI environment.
Result
1
if it looks like the request came from a browser, 0
otherwise.
json_to_html
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.
Parameters
This method expects positional parameters.
- json
-
The JSON byte string.
Result
The JSON wrapped in HTML.
SEE ALSO
AUTHOR
Manfred Stock <mstock@cpan.org>
COPYRIGHT AND LICENSE
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.