-
-
09 Nov 2020 09:19:36 UTC
- Distribution: Amon2
- Source (raw)
- Browse (raw)
- Changes
- Homepage
- How to Contribute
- Repository
- Issues (12)
- Testers (402 / 4 / 2)
- Kwalitee
Bus factor: 1- 69.54% Coverage
- License: perl_5
- Perl: v5.10.0
- Activity
24 month- Tools
- Download (934.99KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
and 35 contributors-
Tokuhiro Matsuno
-
Adrian
-
Fuji, Goro
-
Geraud
-
Geraud CONTINSOUZAS
-
Hiroyuki Akabane
-
ITO Nobuaki
-
Ichito Nagata
-
Katsuhiro Konishi
-
Kazuhiro Osawa
-
Keiji, Yoshimi
-
Kentaro Kuribayashi
-
Koichi SATOH
-
Masahiro Nagano
-
Neil Bowers
-
Nishibayashi Takuji
-
Syohei YOSHIDA
-
Takafumi Ono
-
Yuki Ibe
-
Zak B. Elep
-
clicktx
-
dragon3
-
hatyuki
-
hiratara
-
hisaichi5518
-
issm
-
karupanerura
-
magnolia
-
mattn
-
moznion
-
noblejasper
-
rightgo09
-
s-aska
-
tokubass
-
wata
- Dependencies
- Digest::SHA
- Encode
- Exporter
- File::Copy::Recursive
- File::ShareDir
- File::Temp
- Getopt::Long
- HTML::FillInForm::Lite
- HTTP::Headers
- HTTP::Session2
- Hash::MultiValue
- JSON
- MIME::Base64
- Module::CPANfile
- Plack
- Plack::Request
- Plack::Response
- Plack::Session
- Plack::Util
- Pod::Usage
- Router::Boom
- Scalar::Util
- Text::Xslate
- Time::HiRes
- Try::Tiny
- URI
- URI::Escape
- URI::QueryParam
- mro
- parent
- Reverse dependencies
- CPAN Testers List
- Dependency graph
NAME
Amon2::Plugin::Web::JSON - JSON plugin
SYNOPSIS
use Amon2::Lite; __PACKAGE__->load_plugins(qw/Web::JSON/); get '/' => sub { my $c = shift; return $c->render_json(+{foo => 'bar'}); }; __PACKAGE__->to_app();
DESCRIPTION
This is a JSON plugin.
METHODS
$c->render_json(\%dat);
-
Generate JSON data from
\%dat
and returns instance of Plack::Response.
PARAMETERS
- status_code_field
-
It specify the field name of JSON to be embedded in the 'X-API-Status' header. Default is
undef
. If you set theundef
to disable this 'X-API-Status' header.__PACKAGE__->load_plugins( 'Web::JSON' => { status_code_field => 'status' } ); ... $c->render_json({ status => 200, message => 'ok' }) # send response header 'X-API-Status: 200'
In general JSON API error code embed in a JSON by JSON API Response body. But can not be logging the error code of JSON for the access log of a general Web Servers. You can possible by using the 'X-API-Status' header.
- canonical
-
If canonical parameter is true, then this plugin will output JSON objects by sorting their keys. This is adding a comparatively high overhead.
__PACKAGE__->load_plugins( 'Web::JSON' => { canonical => 1 } ); ... $c->render_json({ b => 1, c => 1, a => 1 }); # json response is '{ "a" : 1, "b" : 1, "c" : 1 }'
FAQ
- How can I use JSONP?
-
You can use JSONP by using Plack::Middleware::JSONP.
JSON and security
- Browse the JSON files directly.
-
This module escapes '<', '>', and '+' characters by "\uXXXX" form. Browser don't detects the JSON as HTML.
And also this module outputs
X-Content-Type-Options: nosniff
header for IEs.It's good enough, I hope.
- JSON Hijacking
-
Latest browsers doesn't have a JSON hijacking issue(I hope). __defineSetter__ or UTF-7 attack was resolved by browsers.
But Firefox<=3.0.x and Android phones have issue on Array constructor, see http://d.hatena.ne.jp/ockeghem/20110907/p1.
Firefox<=3.0.x was outdated. Web application developers doesn't need to add work-around for it, see http://en.wikipedia.org/wiki/Firefox#Version_release_table.
Amon2::Plugin::Web::JSON have a JSON hijacking detection feature. Amon2::Plugin::Web::JSON returns "403 Forbidden" response if following pattern request.
See also the hasegawayosuke's article(Japanese).
FAQ
- HOW DO YOU CHANGE THE HTTP STATUS CODE FOR JSON?
-
render_json method returns instance of Plack::Response. You can modify the response object.
Here is a example code:
get '/' => sub { my $c = shift; if (-f '/tmp/maintenance') { my $res = $c->render_json({err => 'Under maintenance'}); $res->status(503); return $res; } return $c->render_json({err => undef}); };
THANKS TO
hasegawayosuke
Module Install Instructions
To install Amon2, copy and paste the appropriate command in to your terminal.
cpanm Amon2
perl -MCPAN -e shell install Amon2
For more information on module installation, please visit the detailed CPAN module installation guide.