HTTP::Request::JSON - a subclass of HTTP::Request that understands JSON


 my $request = HTTP::Request::JSON->new(PATCH => "$base_url/death_ray");
 # $request has an Accept header saying it's OK to send JSON back
         self_destruct_mechanism   => 'disabled',
         users_allowed_to_override => [],
 # Request content is JSON-encoded, and the content-type is set.


This is a simple subclass of HTTP::Request::JSON that does two things. First of all, it sets the Accept header to application/json as soon as it's created. Secondly, it implements an "add_json_content" method that adds the supplied data structure to the request, as JSON.


 In: ...
 Out: $request

As HTTP::Request->new, but also sets the Accept header.


 In: $perl_data
 Out: $success

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.

All strings in $perl_data must be Unicode strings, or you will get encoding errors.


Sam Kington <>

The source code for this module is hosted on GitHub - this is probably the best place to look for suggestions and feedback.


Copyright (c) 2015 Sam Kington.


This library is free software and may be distributed under the same terms as perl itself.