码神之路:Perl篇

Mojo::UserAgent::Transactor

2017-09-29  本文已影响10人  JSON_NULL

简介

Mojo::UserAgent::Transactor 是Mojo::UserAgent中使用的事务构建和操作框架。

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;

内容生成器

下面是两种在Mojo::UserAgent::Transactor中实现的两个内容生成器,默认情况下是可用的。

form

$t->tx(POST => 'http://example.com' => form => {a => 'b'});

生成HTTP请求中的查询字符,编码为application/x-www-form-urlencodedmultipart/form-data格式的内容。

json

$t->tx(PATCH => 'http://example.com' => json => {a => 'b'});

使用Mojo::JSON对象生成用于在HTTP请求的request体中使用的JSON格式的内容。

属性

generators

my $generators = $t->generators;
$t             = $t->generators({foo => sub {...}});

设置或获取“内容生成器”,默认可用的内容生成器仅有form和json。

name

my $name = $t->name;
$t       = $t->name('Mojolicious');

获取或设置用户代理生成request中请求头User-Agent的值。默认值为Mojolicious (Perl)。

方法

Mojo::UserAgent::Transactor继承了Mojo::Base中的所有方法,并实现了以下方法。

add_generator

$t = $t->add_generator(foo => sub {...});

注册内容生成器。

$t->add_generator(foo => sub {
  my ($t, $tx, @args) = @_;
  ...
});

endpoint

my ($proto, $host, $port) = $t->endpoint(Mojo::Transaction::HTTP->new);

获取事务的实际端口。

peer

my ($proto, $host, $port) = $t->peer(Mojo::Transaction::HTTP->new);

获取事务的实际 peer。

proxy_connect

my $tx = $t->proxy_connect(Mojo::Transaction::HTTP->new);

如果可能,构建一个Mojo::Transaction::HTTP对象用于代理CONNECT请求。

redirect

my $tx = $t->redirect(Mojo::Transaction::HTTP->new);

如果可能,构建一个Mojo::Transaction::HTTP对象用于对状态码301 ,302,303,307,308进行请求重定向。

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'});

使用通用事务构建器Mojo :: Transaction :: HTTP对象发起请求,支持“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内容生成器使用Mojo:: JSON对象编辑数据,并将内容类型application/json。

# POST request with "application/json" content
my $tx = $t->tx(POST => 'http://example.com' => json => {a => 'b', c => [1, 2, 3]});

form内容生成器会自动使用usr中的“查询参数”为GET和HEAD请求中的数据进行编码。

# GET request with query parameters
my $tx = $t->tx(GET => 'http://example.com' => form => {a => 'b'});

对于其他请求方法,使用application/x-www-form-urlencoded内容类型。

# POST request with "application/x-www-form-urlencoded" content
my $tx = $t->tx(POST => 'http://example.com' => form => {a => 'b', c => 'd'});

可以使用charset选项对参数进行编码。

# PUT request with Shift_JIS encoded form values
my $tx = $t->tx(PUT => 'example.com' => form => {a => 'b'} => charset => 'Shift_JIS');

数组引用可用于表示拥有多值的表单项。

# POST request with form values sharing the same name
my $tx = $t->tx(POST => 'http://example.com' => form => {a => ['b', 'c', 'd']});

具有content或file值的表单项可以使用“哈希引用”类型的Perl数据表示;这时使用multipart/form-data格式的内容对要“上传的文件”和“内容”进行编码。

# 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'}]});

在上传文件时,file值应包含要上传的文件的路径或资源对象,例如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可以自动生成也可以手动设置。哈希引用中的其他值都会被合并到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数据的类型也可以通过设置Content-Type头来手动指定。

# 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);

构建一个Mojo::Transaction::WebSocket对象,并开始进行WebSocket事务的握手请求。

websocket

my $tx = $t->websocket('ws://example.com');
my $tx = $t->websocket('ws://example.com' => {DNT => 1} => ['v1.proto']);

使用Mojo::Transaction::HTTP进行WebSocket扬请求的多功能事务构建器。

上一篇下一篇

猜你喜欢

热点阅读