TABLE OF CONTENTS

NAME

Mojo::UserAgent::Transactor - User agent transactor

SYNOPSIS

use Mojo::UserAgent::Transactor;

# Simple GET request
my $t = Mojo::UserAgent::Transactor->new;
say $t->tx(GET => 'http://example.com')->req->to_string;

# PATCH request with "Do Not Track" header and content
say $t->tx(PATCH => 'example.com' => {DNT => 1} => 'Hi!')->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;

DESCRIPTION

Mojo::UserAgent::Transactor is the transaction building and manipulation framework used by Mojo::UserAgent.

GENERATORS

These content generators are available by default.

form

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

Generate query string, application/x-www-form-urlencoded or multipart/form-data content.

json

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

Generate JSON content with Mojo::JSON.

ATTRIBUTES

Mojo::UserAgent::Transactor implements the following attributes.

generators

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

Registered content generators, by default only form and json are already defined.

name

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

Value for User-Agent request header of generated transactions, defaults to Mojolicious (Perl).

METHODS

Mojo::UserAgent::Transactor inherits all methods from Mojo::Base and implements the following new ones.

add_generator

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

Register a new content generator.

endpoint

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

Actual endpoint for transaction.

peer

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

Actual peer for transaction.

proxy_connect

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

Build Mojo::Transaction::HTTP proxy CONNECT request for transaction if possible.

redirect

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

Build Mojo::Transaction::HTTP follow-up request for 301, 302, 303, 307 or 308 redirect response if possible.

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' => 'Hi!');
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 => '*/*'} => 'Hi!');
my $tx = $t->tx(
  PUT  => 'http://example.com' => {Accept => '*/*'} => form => {a => 'b'});
my $tx = $t->tx(
  PUT  => 'http://example.com' => {Accept => '*/*'} => json => {a => 'b'});

Versatile general purpose Mojo::Transaction::HTTP transaction builder for requests, with support for "GENERATORS".

# Generate and inspect custom GET request with DNT header and content
say $t->tx(GET => 'example.com' => {DNT => 1} => 'Bye!')->req->to_string;

# Use a custom socket for processing this transaction
my $tx = $t->tx(GET => 'http://example.com');
$tx->connection($sock);

# 给响应以流的方式打印到标准输出
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 来编码后,并设置 content type 为 application/json. content type to application/json.

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

如果是 form 的内容生成器, 并且请求是 GETHEAD 时会自动的给查询的参数加入到请求中。

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

对于其它的请求,使用使用 application/x-www-form-urlencoded 的 content type.

# 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 => [qw(b c d)]});

如果哈希的引用中,有 content 或者 file 的值,可以使用转换成 multipart/form-data 的 content type, 来用于我们的上传。

# 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 的值来指定包含路径的文件 , 来做为你上传用的 asset 的对象,这会调用 Mojo::Asset::File or 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 中的 http 的 header 中.

# 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 也是可以被强行的通过 Content-Type 这个 header 来手工替换的. Content-Type header manually.

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

Build Mojo::Transaction::WebSocket follow-up transaction for WebSocket handshake if possible.

websocket

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

Versatile Mojo::Transaction::HTTP transaction builder for WebSocket handshake requests.

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.