Plack-Middleware-JSONP-Headers

 view release on metacpan or  search on metacpan

lib/Plack/Middleware/JSONP/Headers.pm  view on Meta::CPAN

                    
                    # this line added
                      $body = $self->wrap_json( $res->[0], $h, $body );

                    my $jsonp = "$cb($body)";
                    $res->[2] = [ $jsonp ];
                    $h->set('Content-Length', length $jsonp);
                    $h->set('Content-Type', 'text/javascript');
                }
            }
        }
    });
}

1;



__END__
=pod

=head1 NAME

Plack::Middleware::JSONP::Headers - Wraps JSON response with HTTP headers in JSONP

=head1 VERSION

version 0.11

=head1 SYNOPSIS

    enable "JSONP::Headers", 
        callback_key => 'callback',
        headers      => qr/^(X-|Link$)/,
        template     => '{ "meta": %s, "data": %s }';

=head1 DESCRIPTION

Plack::Middleware::JSONP::Headers wraps JSON response in JSONP (just like
L<Plack::Middleware::JSONP>) and adds HTTP header information. For instance
the JSON response

    { "foo": "bar" }

with query parameter C<callback> set to C<doz> is wrapped to

    doz({ 
      "meta": { 
        "status": 200, 
        "Content-Type": "application/javascript"
      }, 
      "data": { "foo": "bar" }
    })

The HTTP headers to be wrapped can be configured. All header values are
returned as strings (repeatable headers are ignored) with one exception:
Link-headers are parsed and returned as array of C<<[ url, options ]>>
tuples.

Same as Plack::Middleware::JSONP this middleware only works with a
non-streaming response.

=head1 CONFIGURATION

=over 4

=item callback_key

Callback query parameter. Set to C<callback> by default.

=item headers

List of HTTP headers or regular expression of headers to add to the response.
One can alternatively pass a code reference that gets each header as key-value
pair. By default all headers are wrapped.

=item template

String template for C<sprintf> to construct the JSON response from HTTP headers
(first) and original response (second). Set to C<{ "meta": %s, "data": %s }> by
default.

=back

=head1 SEE ALSO

Inspired by L<http://developer.github.com/v3/#json-p-callbacks>.

=encoding utf8

=head1 AUTHOR

Jakob Voß <voss@gbv.de>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Jakob Voß.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut



( run in 0.491 second using v1.01-cache-2.11-cpan-d8267643d1d )