The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Name

REST::Consumer - General client for interacting with json data in HTTP Restful services

Synopsis

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.

Usage

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',
                },
        );

Methods

get ( path => PATH, params => [] )

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,
                ]
        );
post (path => PATH, params => [key => value, key => value], content => {...} )

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 }
        );
delete (path => PATH, params => [])

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,
                ]
        );
get_response (path => PATH, params => [key => value, key => value], headers => [key => value,....], content => {...}, method => METHOD )

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,
                ],
        );
get_processed_response (path => PATH, params => [key => value, key => value], headers => [key => value,....], content => {...}, method => METHOD)

Send a request with path, params, and content, using the specified method, and get the deserialized content back

get_http_request ( path => PATH, params => [PARAMS], headers => [HEADERS], content => [], method => '' )

get an HTTP::Request object for the given input