NAME
HTTP::Request::JSON - a subclass of HTTP::Request that understands JSON
SYNOPSIS
my $request = HTTP::Request::JSON->new(PATCH => "$base_url/death_ray");
# $request has an Accept header saying it's OK to send JSON back
$request->json_content(
{
self_destruct_mechanism => 'disabled',
users_allowed_to_override => [],
}
);
# Request content is JSON-encoded, and the content-type is set.
DESCRIPTION
This is a simple subclass of HTTP::Request that does two things. First of all, it sets the Accept header to application/json
as soon as it's created. Secondly, it implements a "json_content" method that adds the supplied data structure to the request, as JSON, or returns the current JSON contents as a Perl structure.
new
In: ...
Out: $request
As HTTP::Request->new, but also sets the Accept header.
json_content
In: $perl_data (optional)
Out: $converted_content
A mutator for the request's JSON contents.
As a setter, supplied with a valid JSON data structure, sets the request contents to be the JSON-encoded version of that data structure, and sets the Content-Type header to application/json
. Will throw an exception if the data structure cannot be converted to JSON. Returns the resulting string contents.
All strings in $perl_data must be Unicode strings, or you will get encoding errors.
As a getter, decodes the request contents from JSON into a Perl structure, and returns the resulting data structure.
json_object
Out: $json_object
Returns an object that knows how to handle the encode
and decode
methods. By default whatever LWP::JSON::Tiny->json_object returns. This is what you'd subclass if you wanted to use some other kind of JSON object instead.
AUTHOR
Sam Kington <skington@cpan.org>
The source code for this module is hosted on GitHub https://github.com/skington/lwp-json-tiny - this is probably the best place to look for suggestions and feedback.
COPYRIGHT
Copyright (c) 2015 Sam Kington.
LICENSE
This library is free software and may be distributed under the same terms as perl itself.