Name

SPVM::Mojo::UserAgent::Transactor - User agent transactor

Description

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

Usage

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" => [(object)form => {a => "b"}])->req->to_string;

# PUT request with JSON data
say $t->tx(PUT => "example.com" => [(object)json => {a => "b"}])->req->to_string;

Generators

These content generators are available by default.

form

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

Generate query string, application/x-www-form-urlencoded or multipart/form-data content. See "tx" for more.

json

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

Generate JSON content with JSON. See "tx" for more.

multipart

$t->tx(PUT => "http://example.com" => [(object)multipart => ["Hello", "World!"]]);

Generate multipart content. See "tx" for more.

Fields

compressed

has compressed : rw byte;

Try to negotiate compression for the response content and decompress it automatically, defaults to the value of the SPVM_MOJO_GZIP environment variable or true.

generators

has generators : rw Hash of Mojo::UserAgent::Transactor::Callback::Generator;

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

name

has name : rw string;

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

Class Methods

static method new : Mojo::UserAgent::Transactor ();

Create a new Mojo::UserAgent::Transactor object, and return it.

Instance Methods

add_generator

method add_generator : void ($name : string, $cb : Mojo::UserAgent::Transactor::Callback::Generator);

Register a content generator.

Examples:

$t->add_generator(foo => method : void ($t : Mojo::UserAgent::Transactor, $tx : Mojo::Transaction, $arg : object) {});

download

Not yet implemented.

endpoint

method endpoint : Mojo::UserAgent::Transactor::Endpoint ($tx : Mojo::Transaction);

Actual endpoint for transaction.

peer

method peer : Mojo::UserAgent::Transactor::Endpoint ($tx : Mojo::Transaction::HTTP);

Actual peer for transaction.

proxy_connect

method proxy_connect : Mojo::Transaction::HTTP ($old : Mojo::Transaction::HTTP);

promisify

Not yet implemented.

redirect

method redirect : Mojo::Transaction::HTTP ($old : Mojo::Transaction::HTTP);

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

tx

method tx : Mojo::Transaction::HTTP ($method : string, $url : object of string|Mojo::URL, $args1 : object = undef, $args2 : object = undef);

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

Examples:

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" => [(object)form => {a => "b"}]);
my $tx = $t->tx(PUT  => "http://example.com" => [(object)json => {a => "b"}]);
my $tx = $t->tx(PUT  => "https://example.com" => [(object)multipart => ["a", "b"]]);
my $tx = $t->tx(POST => "example.com" => {Accept => "*/*"} => "Content!");
my $tx = $t->tx(PUT => "example.com" => {Accept => "*/*"} => [(object)form => {a => "b"}]);
my $tx = $t->tx(PUT => "example.com" => {Accept => "*/*"} => [(object)json => {a => "b"}]);
my $tx = $t->tx(PUT => "example.com" => {Accept => "*/*"} => [(object)multipart => ["a", "b"]]);

# 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");
(my $_ = $tx->res->content, $_->unsubscribe("read"), $_->on(read => method : void ($chunk : string) { say $string }));

# PUT request with content streamed from file
my $tx = $t->tx(PUT => "http://example.com");
$tx->req->content->asset((my $_ = Mojo::Asset::File->new, $_->set_path("/foo.txt"), $_));

The json content generator uses JSON for encoding and sets the content type to application/json.

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

The form content generator will automatically use query parameters for GET and HEAD requests.

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

For all other request methods the application/x-www-form-urlencoded content type is used.

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

Parameters may be encoded with the foo option.

# PUT request with Shift_JIS encoded form values
my $tx = $t->tx(PUT => "example.com" => [(object)form => {a => "b"} => {foo => "value"}]);

An array reference can be used for multiple form values sharing the same name.

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

A hash reference with a content or file value can be used to switch to the multipart/form-data content type for file uploads.

# POST request with "multipart/form-data" content
my $tx = $t->tx(POST => "http://example.com" => [(object)form => {mytext => {content => "lala"}]});

# POST request with multiple files sharing the same name
my $tx = $t->tx(POST => "http://example.com" => [(object)form => {mytext => [{content => "first"}, {content => "second"}]]});

The file value should contain the path to the file you want to upload or an asset object, like Mojo::Asset::File or Mojo::Asset::Memory.

# POST request with upload streamed from file
my $tx = $t->tx(POST => "http://example.com" => [(object)form => {mytext => {file => "/foo.txt"}]});

# POST request with upload streamed from asset
my $asset = (my $_ = Mojo::Asset::Memory->new, $_->add_chunk("lalala"), $_);
my $tx    = $t->tx(POST => "http://example.com" => [(object)form => {mytext => {file => $asset}]});

A filename value will be generated automatically, but can also be set manually if necessary. All remaining values in the hash reference get merged into the multipart/form-data content as headers.

# POST request with form values and customized upload (filename and header)
my $tx = $t->tx(POST => "http://example.com" => [(object)form => {
  a      => "b",
  c      => "d",
  mytext => {
    content        => "lalala",
    filename       => "foo.txt",
    "Content-Type" => "text/plain"
  }
}]);

The multipart/form-data content type can also be enforced by setting the Content-Type header manually.

# Force "multipart/form-data"
my $headers = {"Content-Type" => "multipart/form-data"};
my $tx = $t->tx(POST => "example.com" => $headers => [(object)form => {a => "b"}]);

The multipart content generator can be used to build custom multipart requests and does not set a content type.

# POST request with multipart content ("foo" and "bar")
my $tx = $t->tx(POST => "http://example.com" => [(object)multipart => ["foo", "bar"]]);

Similar to the form content generator you can also pass hash references with content or file values, as well as headers.

# POST request with multipart content streamed from file
my $tx = $t->tx(POST => "http://example.com" => [(object)multipart => [{file => "/foo.txt"}]]);

# PUT request with multipart content streamed from asset
my $headers = {"Content-Type" => "multipart/custom"};
my $asset   = (my $_ = Mojo::Asset::Memory->new, $_->add_chunk("lalala"), $_);
my $tx      = $t->tx(PUT => "http://example.com" => $headers => [(object)multipart => [{file => $asset}]]);

# POST request with multipart content and custom headers
my $tx = $t->tx(POST => "http://example.com" => [(object)multipart => [
  {
    content            => "Hello",
    "Content-Type"     => "text/plain",
    "Content-Language" => "en-US"
  },
  {
    content            => "World!",
    "Content-Type"     => "text/plain",
    "Content-Language" => "en-US"
  }
]]);

upgrade

method upgrade : Mojo::Transaction::WebSocket ($tx : Mojo::Transaction::HTTP);

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

See Also

Copyright & License

Copyright (c) 2025 Yuki Kimoto

MIT License