LWP::UserAgent::JSON - a subclass of LWP::UserAgent that understands JSON
my $user_agent = LWP::UserAgent::JSON->new; my $request = HTTP::Request::JSON->new(...); my $response = $user_agent->request($request); # $response->isa('HTTP::Response::JSON') if we got back JSON
This is a subclass of LWP::UserAgent which recognises if it gets JSON output back, and if so returns an HTTP::Response::JSON object instead of a HTTP::Response object. It exposes the logic of reblessing the HTTP::Response object in case you get handed a HTTP::Response object by some other method.
It also offers a handful of convenience methods to directly convert parameters into JSON for POST, PUT and PATCH requests.
Like LWP::UserAgent::post, except for when it's called as
post_json($url, $form_ref, ...), in which case $form_ref is turned into JSON. Obviously if you specify Content-Type or Content in subsequent header arguments they'll take precedence.
A variant on LWP::UserAgent::put with the same transformations as post_json. This requires that your version of LWP supports PUT, i.e. you have LWP 6.00 or later.
As post_json and put_json, but generates a PATCH request instead. As put_json, you need a semi-modern version of LWP for this.
LWP::UserAgent doesn't actually implement a patch method, so it's defined here.
In: $object Out: $reblessed
Supplied with a HTTP::Request or HTTP::Response object, looks to see if it's a JSON object, and if so reblesses it to be a HTTP::Request::JSON or HTTP::Response::JSON object respectively. Returns whether it reblessed the object or not.
Sam Kington <email@example.com>
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 (c) 2015 Sam Kington.
This library is free software and may be distributed under the same terms as perl itself.