Manfred Stock > Plack-Middleware-JSON-ForBrowsers-0.001000 > Plack::Middleware::JSON::ForBrowsers

Download:
Plack-Middleware-JSON-ForBrowsers-0.001000.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.001000   Source   Latest Release: Plack-Middleware-JSON-ForBrowsers-0.002000

NAME ^

Plack::Middleware::JSON::ForBrowsers - Plack middleware which turns application/json responses into HTML

VERSION ^

version 0.001000

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;
        };

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.

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) 2012 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.

syntax highlighting: