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.


As post_json and put_json, but generates a PATCH request instead.


LWP::UserAgent doesn't actually implement a patch method, so it's defined here.


As LWP::UserAgent::simple_request, but returns a HTTP::Response:JSON object instead of a HTTP::Response object if the response is JSON.


 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 <>

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.