Mojo::UserAgent::Transactor - ユーザーエージェントトランザクター
使い方
use Mojo::UserAgent::Transactor;
# AcceptヘッダーのついたGETリクエスト
my $t = Mojo::UserAgent::Transactor->new;
say $t->tx(GET => 'http://example.com' => {Accept => '*/*'})->req->to_string;
# "Do Not Track"ヘッダーとコンテンツを持ったPATCHリクエスト
say $t->tx(PATCH => 'example.com' => {DNT => 1} => 'Hi!')->req->to_string;
# フォームデータを持ったPOSTリクエスト
say $t->tx(POST => 'example.com' => form => {a => 'b'})->req->to_string;
# JSONデータを持ったPUTリクエスト
say $t->tx(PUT => 'example.com' => json => {a => 'b'})->req->to_string;
説明
Mojo::UserAgent::TransactorはMojo::UserAgentのための トランザクションの構築と処理のためのフレームワークです。
ジェネレーター
以下のジェネレーターがデフォルトで利用可能です。
form
$t->tx(POST => 'http://example.com' => form => {a => 'b'});
クエリ文字列、application/x-www-form-urlencodedあるいは、 multipart/form-dataコンテンツを生成します。
json
$t->tx(PATCH => 'http://example.com' => json => {a => 'b'});
Mojo::JSONを使ってJSONコンテンツを生成します。
multipart
$t->tx(PUT => 'http://example.com' => multipart => ['Hello', 'World!']);
マルチパートコンテントを生成します。詳しくは"tx"を見てください。
属性
Mojo::UserAgent::Transactorは次の属性を実装しています。
compressed
my $bool = $t->compressed; $t = $t->compressed($bool);
Try to negotiate compression for the response content and decompress it automatically, defaults to the value of the MOJO_GZIP environment variable or true.
応答コンテンツの圧縮を交渉し、自動的に解凍を試みます。 デフォルトはMOJO_GZIP環境変数の値、あるいは真です。
generators
my $generators = $t->generators;
$t = $t->generators({foo => sub {...}});
コンテンツのジェネレーターを登録します。 デフォルトでformとjsonとmultipartがすでに定義されています。
name
my $name = $t->name;
$t = $t->name('Mojolicious');
生成されたトランザクションのUser-Agentリクエストヘッダの値。デフォルトはMojolicious (Perl)です。
メソッド
Mojo::UserAgent::TransactorはMojo::Baseからすべてのメソッドを継承しており、 次の新しいメソッドを実装しています。
add_generator
$t = $t->add_generator(foo => sub {...});
新しいコンテンツジェネレーターを登録します。
endpoint
my ($proto, $host, $port) = $t->endpoint(Mojo::Transaction::HTTP->new);
トランザクションの実際のエンドポイント。
peer
my ($proto, $host, $port) = $t->peer(Mojo::Transaction::HTTP->new);
トランザクションの実際のあて先。
promisify
$t->promisify(Mojo::Promise->new, Mojo::Transaction::HTTP->new);
Mojo::Promiseオブジェクトを、Mojo::Transaction::HTTPオブジェクトで解決または拒否します。
proxy_connect
my $tx = $t->proxy_connect(Mojo::Transaction::HTTP->new);
可能であればトランザクションのためにMojo::Transaction::HTTPプロキシ接続のリクエスト を構築します。
redirect
my $tx = $t->redirect(Mojo::Transaction::HTTP->new);
可能であれば301, 302, 303 ,307リダイレクトレスポンスのための Mojo::Transaction::HTTP追跡リクエストを構築します。
tx
my $tx = $t->tx(GET => 'example.com');
my $tx = $t->tx(POST => 'http://example.com');
my $tx = $t->tx(GET => 'http://example.com' => {Accept => '*/*'});
my $tx = $t->tx(PUT => 'http://example.com' => 'Content!');
my $tx = $t->tx(PUT => 'http://example.com' => form => {a => 'b'});
my $tx = $t->tx(PUT => 'http://example.com' => json => {a => 'b'});
my $tx = $t->tx(PUT => 'https://example.com' => multipart => ['a', 'b']);
my $tx = $t->tx(POST => 'example.com' => {Accept => '*/*'} => 'Content!');
my $tx = $t->tx(
PUT => 'example.com' => {Accept => '*/*'} => form => {a => 'b'});
my $tx = $t->tx(
PUT => 'example.com' => {Accept => '*/*'} => json => {a => 'b'});
my $tx = $t->tx(
PUT => 'example.com' => {Accept => '*/*'} => multipart => ['a', 'b']);
コンテンツジェネレーターをサポートした、 リクエストのための、用途の広い一般的な目的の Mojo::Transaction::HTTPトランザクションビルダー。
# DNTヘッダーとコンテンツを持ったカスタムGETリクエストを生成して覗く。
say $t->tx(GET => 'example.com' => {DNT => 1} => 'Bye!')->req->to_string;
# レスポンスコンテンツをSTDOUTにストリームする
my $tx = $t->tx(GET => 'http://example.com');
$tx->res->content->unsubscribe('read')->on(read => sub { say $_[1] });
# ファイルからコンテンツをストリームしたPUTリクエスト
my $tx = $t->tx(PUT => 'http://example.com');
$tx->req->content->asset(Mojo::Asset::File->new(path => '/foo.txt'));
jsonコンテンツジェネレーターはエンコードのためにMojo::JSONを使用し、 コンテンツタイプをapplication/jsonに設定します。
# "application/json"コンテンツのついたPOSTリクエスト
my $tx = $t->tx(
POST => 'http://example.com' => json => {a => 'b', c => [1, 2, 3]});
formコンテンツジェネレーターは自動的にクエリ文字列を GETとHEADのリクエストのために使用します。
# クエリ文字列のついてGETリクエスト
my $tx = $t->tx(GET => 'http://example.com' => form => {a => 'b'});
すべての他のリクエストメソッドでは、 application/x-www-form-urlencodedコンテントタイプが使用されます。
# "application/x-www-form-urlencoded"コンテンツがついたPOSTリクエスト
my $tx = $t->tx(
POST => 'http://example.com' => form => {a => 'b', c => 'd'});
パラメーターはcharsetオプションでエンコードすることもできます。.
# SHIFT_JISにエンコードされたフォームの値がついたPUTリクエスト
my $tx = $t->tx(
PUT => 'example.com' => form => {a => 'b'} => charset => 'Shift_JIS');
配列のリファレンスは同じ名前で共有される複数のフォームの値のために利用可能です。
# 同じ名前を共有しているフォームの値を持ったPOSTリクエスト
my $tx = $t->tx(
POST => 'http://example.com' => form => {a => ['b', 'c', 'd']});
contentかfileの値が設定されたハッシュリファレンスは、 コンテントタイプをmultipart/form-dataに変更し、ファイルアップロードのために、 利用可能です。
# "multipart/form-data"コンテンツのついたPOSTリクエスト
my $tx = $t->tx(
POST => 'http://example.com' => form => {mytext => {content => 'lala'}});
# 同じ名前で共有される複数のファイルのPOSTリクエスト
my $tx = $t->tx(POST => 'http://example.com' =>
form => {mytext => [{content => 'first'}, {content => 'second'}]});
fileの値は、アップロードしたいファイルのパスを含んでいるか、Mojo::Asset::FileやMojo::Asset::Memoryのようなアセットオブジェクトであるべきです。
# アップロードがファイルからストリームされるPOSTリクエスト
my $tx = $t->tx(
POST => 'http://example.com' => form => {mytext => {file => '/foo.txt'}});
# アップロードがアッセトからストリームされるPOSTリクエスト
my $asset = Mojo::Asset::Memory->new->add_chunk('lalala');
my $tx = $t->tx(
POST => 'http://example.com' => form => {mytext => {file => $asset}});
filenameの値は、自動的に生成されますが、 必要であれば、手動で設定できます。 ハッシュのすべての残りの値は、ヘッダとしてmultipart/form-dataにマージされます。
# フォームの値とカスタマイズされたアップロード(ファイル名とヘッダ)がついたPOSTリクエスト
my $tx = $t->tx(POST => 'http://example.com' => form => {
a => 'b',
c => 'd',
mytext => {
content => 'lalala',
filename => 'foo.txt',
'Content-Type' => 'text/plain'
}
});
multipart/form-dataコンテントタイプは Content-Typeヘッダを強制的に設定するのに利用できます。
# "multipart/form-data"の強制
my $headers = {'Content-Type' => 'multipart/form-data'};
my $tx = $t->tx(POST => 'example.com' => $headers => form => {a => 'b'});
The multipart content generator can be used to build custom multipart requests and does not set a content type.
C
# マルチパートコンテンツでリクエストをポスト("foo" and "bar")
my $tx = $t->tx(POST => 'http://example.com' => multipart => ['foo', 'bar']);
Similar to the formコンテンツジェネレーターと似ていて、 ヘッダーと同じように、contentかfileの値のハッシュリファレンスを渡すことができます。
# ファイルからストリームされたマルチパートコンテンツでリクエストをPOST
my $tx = $t->tx(
POST => 'http://example.com' => multipart => [{file => '/foo.txt'}]);
# アセットからストリームされたマルチパートコンテンツでリクエストをPOST
my $headers = {'Content-Type' => 'multipart/custom'};
my $asset = Mojo::Asset::Memory->new->add_chunk('lalala');
my $tx = $t->tx(
PUT => 'http://example.com' => $headers => multipart => [{file => $asset}]);
# マルチパートコンテンツとカスタムヘッダーでリクエストをPOST
my $tx = $t->tx(POST => 'http://example.com' => multipart => [
{
content => 'Hello',
'Content-Type' => 'text/plain',
'Content-Language' => 'en-US'
},
{
content => 'World!',
'Content-Type' => 'text/plain',
'Content-Language' => 'en-US'
}
]);
upgrade
my $tx = $t->upgrade(Mojo::Transaction::HTTP->new);
可能であればMojo::Transaction::WebSocketをフォローアップするWebSocketハンドシェイクトランザクションを構築します。
websocket
my $tx = $t->websocket('ws://example.com');
my $tx = $t->websocket('ws://example.com' => {DNT => 1} => ['v1.proto']);
WebSocketハンドシェイクリクエストのための 多用途のMojo::Transaction::WebSocketのビルダー。
参考
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
(Mojolicious 8.12を反映。2019年5月30日更新)
Mojoliciousドキュメント日本語訳