Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Last active August 29, 2015 13:57
Show Gist options
  • Save ishiduca/9484996 to your computer and use it in GitHub Desktop.
Save ishiduca/9484996 to your computer and use it in GitHub Desktop.
HTTPClient based Tatusmaki::HTTPClient
package Moco::Moco::HTTPClient;
use Moo;
use MooX::late;
use Moco::Moco;
use AnyEvent::HTTP;
extends qw(Tatsumaki::HTTPClient);
has agent => ( is => 'rw', isa => 'Str', default => sub { join '/', __PACKAGE__ , $Moco::Moco::VERSION });
has jar => ( is => 'rw', isa => 'HashRef', default => sub { +{version => 1} });
has recurse => ( is => 'rw', isa => 'Num', default => $AnyEvent::HTTP::MAX_RECURSE);
sub request {
my($self, $request, $cb) = @_;
my $headers = $request->headers;
$headers->{'user-agent'} = $self->agent;
delete $headers->{'content-length'}; # AnyEven::HTTP::request が面倒うみてくれるので
my $jar = $self->jar;
my %options = (
timeout => $self->timeout,
headers => $headers,
cookie_jar => $jar,
recurse => $self->recuse,
body => $request->content,
);
AnyEvent::HTTP::http_request $request->method, $request->uri, %options, sub {
my($body, $header) = @_;
my $res = HTTP::Response->new($header->{Status}, $header->{Reason}, [ %$header ], $body);
$self->jar( $jar );
$cb->($res);
};
}
1;
@ishiduca
Copy link
Author

何かしらのデータをポストした際に、サーバーからリダイレクトを支持された場合、recurse をデフォルトにしておくと最初のヘッダ情報もリダイレクトURLに送っちゃってるっぽいので、recurse => 0 にして手動でリダイレクト先にリクエストを送らないといけないっぽい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment