REST::Consumer - General client for interacting with json data in HTTP Restful services
This module provides an interface that encapsulates building an http request, sending the request, and parsing a json response. It also retries on failed requests and has configurable timeouts.
To make a request, create an instance of the client and then call the get(), post(), put(), or delete() methods
# Required parameters: my $client = REST::Consumer->new( host => 'service.somewhere.com', port => 80, ); # Optional parameters: my $client = REST::Consumer->new( host => 'service.somewhere.com', port => 80, timeout => 60, (default 10) retry => 10, (default 3) verbose => 1, (default 0) keep_alive => 1, (default 0) agent => 'Service Monkey', (default REST-Consumer/$VERSION) auth => { type => 'basic', username => 'yep', password => 'nope', }, );
Send a GET request to the given path with the given arguments
my $deserialized_result = $client->get( path => '/path/to/resource', params => { field => value, field2 => value2, }, );
the 'params' arg can be a hash ref or array ref, depending on whether you need multiple instances of the same key
my $deserialized_result = $client->get( path => '/path/to/resource', params => [ field => value, field => value2, field => value3, ] );
Send a POST request with the given path, params, and content. The content must be a data structure that can be serialized to JSON.
# content is serialized to json by default my $deserialized_result = $client->post( path => '/path/to/resource', content => { field => value } ); # if you don't want it serialized, specify another content type my $deserialized_result = $client->post( path => '/path/to/resource', content => { field => value } content_type => 'multipart/form-data', ); # you can also specify headers if needed my $deserialized_result = $client->post( path => '/path/to/resource', headers => [ 'x-custom-header' => 'monkeys', ], content => { field => value } );
Send a DELETE request to the given path with the given arguments
my $result = $client->delete( path => '/path/to/resource', params => [ field => value, field2 => value2, ] );
Send a request with path, params, and content, using the specified method, and get a response object back
my $response_obj = $client->get_response( method => 'GET', path => '/path/to/resource', headers => [ 'x-header' => 'header', ], params => [ field => value, field2 => value2, ], );
Send a request with path, params, and content, using the specified method, and get the deserialized content back
get an HTTP::Request object for the given input
To install REST::Consumer, copy and paste the appropriate command in to your terminal.
cpanm
cpanm REST::Consumer
CPAN shell
perl -MCPAN -e shell install REST::Consumer
For more information on module installation, please visit the detailed CPAN module installation guide.