簡介
Mojo::UserAgent::Transactor 是Mojo::UserAgent中使用的事務(wù)構(gòu)建和操作框架宝泵。
use Mojo::UserAgent::Transactor;
# GET request with Accept header
my $t = Mojo::UserAgent::Transactor->new;
say $t->tx(GET => 'http://example.com' => {Accept => '*/*'})->req->to_string;
# POST request with form-data
say $t->tx(POST => 'example.com' => form => {a => 'b'})->req->to_string;
# PUT request with JSON data
say $t->tx(PUT => 'example.com' => json => {a => 'b'})->req->to_string;
內(nèi)容生成器
下面是兩種在Mojo::UserAgent::Transactor中實(shí)現(xiàn)的兩個(gè)內(nèi)容生成器轧飞,默認(rèn)情況下是可用的。
form
$t->tx(POST => 'http://example.com' => form => {a => 'b'});
生成HTTP請求中的查詢字符葵陵,編碼為application/x-www-form-urlencoded
或 multipart/form-data
格式的內(nèi)容。
json
$t->tx(PATCH => 'http://example.com' => json => {a => 'b'});
使用Mojo::JSON對象生成用于在HTTP請求的request體中使用的JSON格式的內(nèi)容。
屬性
generators
my $generators = $t->generators;
$t = $t->generators({foo => sub {...}});
設(shè)置或獲取“內(nèi)容生成器”援雇,默認(rèn)可用的內(nèi)容生成器僅有form和json吮铭。
name
my $name = $t->name;
$t = $t->name('Mojolicious');
獲取或設(shè)置用戶代理生成request中請求頭User-Agent
的值时迫。默認(rèn)值為Mojolicious (Perl)。
方法
Mojo::UserAgent::Transactor繼承了Mojo::Base中的所有方法谓晌,并實(shí)現(xiàn)了以下方法掠拳。
add_generator
$t = $t->add_generator(foo => sub {...});
注冊內(nèi)容生成器。
$t->add_generator(foo => sub {
my ($t, $tx, @args) = @_;
...
});
endpoint
my ($proto, $host, $port) = $t->endpoint(Mojo::Transaction::HTTP->new);
獲取事務(wù)的實(shí)際端口纸肉。
peer
my ($proto, $host, $port) = $t->peer(Mojo::Transaction::HTTP->new);
獲取事務(wù)的實(shí)際 peer溺欧。
proxy_connect
my $tx = $t->proxy_connect(Mojo::Transaction::HTTP->new);
如果可能喊熟,構(gòu)建一個(gè)Mojo::Transaction::HTTP對象用于代理CONNECT請求。
redirect
my $tx = $t->redirect(Mojo::Transaction::HTTP->new);
如果可能姐刁,構(gòu)建一個(gè)Mojo::Transaction::HTTP對象用于對狀態(tài)碼301 芥牌,302,303聂使,307壁拉,308進(jìn)行請求重定向。
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(POST => 'http://example.com' => {Accept => '*/*'} => 'Content!');
my $tx = $t->tx(PUT => 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
my $tx = $t->tx(PUT => 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});
使用通用事務(wù)構(gòu)建器Mojo :: Transaction :: HTTP對象發(fā)起請求柏靶,支持“GENERATORS”弃理。
# Generate and inspect custom GET request with DNT header and content
say $t->tx(GET => 'example.com' => {DNT => 1} => 'Bye!')->req->to_string;
# Stream response content to STDOUT
my $tx = $t->tx(GET => 'http://example.com');
$tx->res->content->unsubscribe('read')->on(read => sub { say $_[1] });
# PUT request with content streamed from file
my $tx = $t->tx(PUT => 'http://example.com');
$tx->req->content->asset(Mojo::Asset::File->new(path => '/foo.txt'));
json內(nèi)容生成器使用Mojo:: JSON對象編輯數(shù)據(jù),并將內(nèi)容類型application/json屎蜓。
# POST request with "application/json" content
my $tx = $t->tx(POST => 'http://example.com' => json => {a => 'b', c => [1, 2, 3]});
form內(nèi)容生成器會(huì)自動(dòng)使用usr中的“查詢參數(shù)”為GET和HEAD請求中的數(shù)據(jù)進(jìn)行編碼案铺。
# GET request with query parameters
my $tx = $t->tx(GET => 'http://example.com' => form => {a => 'b'});
對于其他請求方法,使用application/x-www-form-urlencoded內(nèi)容類型梆靖。
# POST request with "application/x-www-form-urlencoded" content
my $tx = $t->tx(POST => 'http://example.com' => form => {a => 'b', c => 'd'});
可以使用charset選項(xiàng)對參數(shù)進(jìn)行編碼控汉。
# PUT request with Shift_JIS encoded form values
my $tx = $t->tx(PUT => 'example.com' => form => {a => 'b'} => charset => 'Shift_JIS');
數(shù)組引用可用于表示擁有多值的表單項(xiàng)。
# POST request with form values sharing the same name
my $tx = $t->tx(POST => 'http://example.com' => form => {a => ['b', 'c', 'd']});
具有content或file值的表單項(xiàng)可以使用“哈希引用”類型的Perl數(shù)據(jù)表示返吻;這時(shí)使用multipart/form-data格式的內(nèi)容對要“上傳的文件”和“內(nèi)容”進(jìn)行編碼姑子。
# POST request with "multipart/form-data" content
my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => {content => 'lala'}});
# POST request with multiple files sharing the same name
my $tx = $t->tx(POST => 'http://example.com' =>
form => {mytext => [{content => 'first'}, {content => 'second'}]});
在上傳文件時(shí),file值應(yīng)包含要上傳的文件的路徑或資源對象测僵,例如Mojo :: Asset :: File或Mojo :: Asset :: Memory街佑。
# POST request with upload streamed from file
my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => {file => '/foo.txt'}});
# POST request with upload streamed from asset
my $asset = Mojo::Asset::Memory->new->add_chunk('lalala');
my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => {file => $asset}});
filename可以自動(dòng)生成也可以手動(dòng)設(shè)置。哈希引用中的其他值都會(huì)被合并到multipart/form-data頭部信息中捍靠。
# POST request with form values and customized upload (filename and header)
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數(shù)據(jù)的類型也可以通過設(shè)置Content-Type頭來手動(dòng)指定沐旨。
# Force "multipart/form-data"
my $headers = {'Content-Type' => 'multipart/form-data'};
my $tx = $t->tx(POST => 'example.com' => $headers => form => {a => 'b'});
upgrade
my $tx = $t->upgrade(Mojo::Transaction::HTTP->new);
構(gòu)建一個(gè)Mojo::Transaction::WebSocket對象,并開始進(jìn)行WebSocket事務(wù)的握手請求榨婆。
websocket
my $tx = $t->websocket('ws://example.com');
my $tx = $t->websocket('ws://example.com' => {DNT => 1} => ['v1.proto']);
使用Mojo::Transaction::HTTP進(jìn)行WebSocket揚(yáng)請求的多功能事務(wù)構(gòu)建器磁携。