Mojo::UserAgent::Transactor

简介

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扬请求的多功能事务构建器。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • 简介 Mojo::UserAgent 是一个全功能的非阻塞 I/O HTTP 和 WebSocket 的用户代理,...
    JSON_NULL阅读 1,115评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,360评论 6 343
  • 简介 Mojolicious::Controller是Mojolicious应用程序中控制器的基类。如果你没有在M...
    JSON_NULL阅读 437评论 0 0
  • 简介 Mojo :: Message是基于RFC 7230,RFC 7231和RFC 2388的 HTTP消息容器...
    JSON_NULL阅读 395评论 0 0