NAME
Hijk - Specialized HTTP client
SYNOPSIS
A simple GET request:
use Hijk;
my $res = Hijk::request({
method => "GET",
host => "example.com",
port => "80",
path => "/flower",
query_string => "color=red"
});
die unless ($res->{status} == "200");
say $res->{body};
A POST request, you have to manually set the appropriate headers, URI escape your values etc.
use Hijk;
use URI::Escape qw(uri_escape);
my $res = Hijk::request({
method => "POST",
host => "example.com",
port => "80",
path => "/new",
head => [ "Content-Type" => "application/x-www-form-urlencoded" ],
query_string => "type=flower&bucket=the%20one%20out%20back",
body => "description=" . uri_escape("Another flower, let's hope it's exciting"),
});
die unless ($res->{status} == "200");
DESCRIPTION
Hijk is a specialized HTTP Client that does nothing but transporting the response body back. It does not feature as a "user agent", but as a dumb client. It is suitble for connecting to data servers transporting via HTTP rather then web servers.
Most of HTTP features like proxy, redirect, Transfer-Encoding, or SSL are not supported at all. For those requirements we already have many good HTTP clients like HTTP::Tiny, Furl or LWP::UserAgent.
FUNCTIONS
Hijk::request( $args :HashRef ) :HashRef
This is the only function to be used. It is not exported to its caller namespace at all. It takes a request arguments in HashRef and returns the response in HashRef.
The $args
request arg should contain key-value pairs from the following table. The value for host
and port
are mandatory and others are optional with default values listed below
- host => ...
- port => ...
- timeout => 0
- method => "GET"
- path => "/"
- query_string => ""
- head => []
- body => ""
- fetch => ...
-
An optional subroutine reference we'll use to do to the fetching. Load Hijk::HTTP::XS and set it to
\&Hijk::HTTP::XS::fetch
to use an XS fetcher.
Too keep the implementation straight-forward, Hijk does not take full URL string as input.
The value of head
is an ArrayRef of key-value pairs instead of HashRef, this way the order of headers can be maintained. For example:
head => [
"Content-Type" => "application/json",
"X-Requested-With" => "Hijk",
]
... will produce these request headers:
Content-Type: application/json
X-Requested-With: Hijk
Again, there are no extra character-escaping filter within Hijk.
The value of timeout
is in seconds, and is used as the time limit for both connecting to the host, and reading from the socket. The default value 0
means that there is no timeout limit. If the host is really unreachable, it will reach the system TCP timeout limit then dies.
The return vaue is a HashRef representing a response. It contains the following key-value pairs.
For example, to send request to http://example.com/flower?color=red
, use the following code:
my $res = Hijk::request({
host => "example.com",
port => "80",
path => "/flower",
query_string => "color=red"
});
die "Response is not OK" unless $res->{status} ne "200";
Notice that you do not need to put the leading "?"
character in the query_string
. You do, however, need to propery uri_escape
the content of query_string
.
All values are assumed to be valid. Hijk simply passthru the values without validating the content. It is possible that it constructs invalid HTTP Messages. Users should keep this in mind when using Hijk.
Noticed that the head
in the response is a HashRef rather then an ArrayRef. This makes it easier to retrieve specific header fields.
We currently don't support returning a body without a Content-Length header, bodies MUST have an accompanying Content-Length or we won't pick them up.
AUTHORS
COPYRIGHT
Copyright (c) 2013 Kang-min Liu <gugod@gugod.org>
.
LICENCE
The MIT License
DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.