NAME

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

VERSION

version 0.11

SYNOPSIS

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

DESCRIPTION

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

{ "foo": "bar" }

with query parameter callback set to 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 <[ url, options ]> tuples.

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

CONFIGURATION

callback_key

Callback query parameter. Set to callback by default.

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.

template

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

SEE ALSO

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

AUTHOR

Jakob Voß <voss@gbv.de>

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.