};
}
sub body_json {
my ($self) = @_;
unless (exists $self->{body_json}) {
$self->{body_json} = undef;
if ($ENV{CONTENT_TYPE} and $ENV{CONTENT_TYPE} =~ m/^application\/json\b/i) {
$
self->{body_json} = $self->_json->decode($self->body);
}
}
return $self->{body_json};
}
sub body_params { [map { [@$_] } @{$_[0]->_body_params->{ordered}}] }
sub body_param_names { [@{$_
) {
return 0+$1;
}
return 200;
}
{
my %RENDER_TYPES = (text => 1, html => 1, xml => 1, json => 1, data => 1, file => 1, handle => 1, redirect => 1);
sub render {
my ($self, $type, $d
ipt.source.cgi > script.cgi
To pack in optional modules, such as JSON support for Perls older than 5.14:
$ fatpack trace --use=JSON::PP script.source.cgi
$ fatpack packlists-for $(cat fatpacker.
=head2 JSON
CGI::Tiny has built in support for parsing and rendering JSON content with
L<JSON::PP>. CGI scripts that deal with JSON content will greatly benefit from
installing L<Cpanel::JSON::XS> v
es can be encoded to JSON and base64 for transport.
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use CGI::Tiny;
use Cpanel::JSON::XS qw(decode_json encode_json);
use MIME::Base64
== 413) {
$cgi->render(json => {error => 'Request body limit exceeded'});
} elsif ($cgi->response_status_code == 400) {
$cgi->render(json => {error => 'Bad request'});
} else {
$cgi->render(json => {error => 'Internal server error'});
}
}
});
my $method = $cgi->method;
my $fribble;
if ($method eq 'GET' or $method eq 'HEA
->param('download')) {
$cgi->set_response_disposition(attachment => 'fribble.json');
}
$cgi->render(json => {fribble => $fribble});
};
=head1 DESCRIPTION
CGI::Tiny provides a modern