JSON-API
view release on metacpan or search on metacpan
This is a hash of options that can be passed in to an LWP object. Additionally, the user, pass, and realm may be provided to configure authentication for LWP. You must provide all three
parameters for authentication to work properly.
Specifying debug => 1 in the parameters hash will also enable debugging output within JSON::API.
Additionally you can specify predecodehook in the parameters hash with a reference to a subroutine. The subroutine will then be called with the received raw content as only
parameter before it is decoded. It then can preprocess the content e.g. alter it to be valid json. An example use case for this is calling a JSON API that prefixes the json with garbage
to prevent CSRF. The pre-decode hook can then strip the garbage from the raw content before the JSON data is being decoded.
get|post|put|del
Perform an HTTP action (GET|POST|PUT|DELETE) against the given API. All methods take the path to the API endpoint as the first parameter. The put() and post() methods also accept a second data
parameter, which should be a reference to be serialized into JSON for POST/PUTing to the endpoint.
If called in scalar context, returns the deserialized JSON content returned by the server. If no content was returned, returns an empty hashref. To check for errors, call errstr or was_success.
If called in list context, returns a two-value array. The first value will be the HTTP response code for the request. The second value will either be the deserialized JSON data. If no data is
returned, returns an empty hashref.
get
Performs an HTTP GET on the given path. path will be appended to the base_url provided when creating this object. If given a data object, this will be turned into querystring parameters, with URI
encoded values.
lib/JSON/API.pm view on Meta::CPAN
content e.g. alter it to be valid json. An example use case for this is calling
a JSON API that prefixes the json with garbage to prevent CSRF. The pre-decode
hook can then strip the garbage from the raw content before the JSON data is
being decoded.
=back
=head2 get|post|patch|put|del
Perform an HTTP action (GET|POST|PATCH|PUT|DELETE) against the given API. All methods
take the B<path> to the API endpoint as the first parameter. The B<patch()>, B<put()> and
B<post()> methods also accept a second B<data> parameter, which should be a reference
to be serialized into JSON for POST/PATCH/PUTing to the endpoint.
All methods also accept an optional B<apphdr> parameter in the last position, which
is a hashref. The referenced hash contains header names and values that will be
submitted with the request. See HTTP::Headers. This can be used to provide
B<If-Modified> or other headers required by the API.
If called in scalar context, returns the deserialized JSON content returned by
the server. If no content was returned, returns an empty hashref. To check for errors,
call B<errstr> or B<was_success>.
t/requests.t view on Meta::CPAN
else { # 404 catchall # {{{
[
404,
[ 'Content-Type' => 'application/json' ],
[ '{"error":"My Custom Page Not Found Message"}' ]
]
} # }}}
};
};
my $api = JSON::API->new($httpd->endpoint, debug => 0);
isa_ok($api, 'JSON::API', "JSON::API obj creation succssful");
call_api($api, "GET", '/get_valid_json', undef,
{name => 'foo', value => 'bar'}, 200,
"get('/get_valid_json') returns hashref decoded from json");
call_api($api, "GET", '/get_valid_json', { name => 'foo', value => 'abc!@#$%^&=?/' },
{success => "query params passed + encoded"}, 200,
"get('/get_valid_json') with query params object passes + encodes params");
t/requests.t view on Meta::CPAN
call_api($api, "GET", '/get_404', undef,
{ error => 'My Custom Page Not Found Message'}, 404,
'get(/get_404) returns page not found');
is_deeply($api->errstr, '{"error":"My Custom Page Not Found Message"}', "get('/get_404') returned an errrstr");
call_api($api, "GET", '/auth-test', undef,
{ error => 'authentication required'}, 401,
'get(/auth-test) with no creds should get auth required failure');
$api = JSON::API->new($httpd->endpoint, debug => 0,
user => 'testuser', pass => 'testbadpass', realm => 'Test');
call_api($api, "GET", '/auth-test', undef,
{ error => 'authentication failed'}, 403,
'get(/auth-test) with bad creds should get authentication failed error');
$api = JSON::API->new($httpd->endpoint, debug => 0,
user => 'testuser', pass => 'testpass', realm => 'Test');
call_api($api, "GET", '/auth-test', undef,
{ code => 'authentication successful'}, 200,
'get(/auth-test) with good creds should succeed');
done_testing;
( run in 1.664 second using v1.01-cache-2.11-cpan-524268b4103 )