Mojo::UserAgent::Transactor - User agent transactor
use Mojo::UserAgent::Transactor; # Simple GET request my $t = Mojo::UserAgent::Transactor->new; say $t->tx(GET => 'http://mojolicio.us')->req->to_string; # PATCH request with "Do Not Track" header and content say $t->tx(PATCH => 'mojolicio.us' => {DNT => 1} => 'Hi!')->req->to_string; # POST request with form data say $t->form('http://kraih.com' => {a => [1, 2], b => 3})->req->to_string; # POST request with JSON data say $t->json('http://kraih.com' => {a => [1, 2], b => 3})->req->to_string;
Mojo::UserAgent::Transactor is the transaction building and manipulation framework used by Mojo::UserAgent.
Mojo::UserAgent::Transactor inherits all methods from Mojo::Base and implements the following new ones.
endpoint
my ($scheme, $host, $port) = $t->endpoint(Mojo::Transaction::HTTP->new);
Actual endpoint for transaction.
form
my $tx = $t->form('kraih.com' => {a => 'b'}); my $tx = $t->form('http://kraih.com' => {a => 'b'}); my $tx = $t->form('http://kraih.com' => {a => [qw(b c d)]}); my $tx = $t->form('http://kraih.com' => {mytext => {file => '/foo.txt'}}); my $tx = $t->form('http://kraih.com' => {mytext => {content => 'lalala'}}); my $tx = $t->form('http://kraih.com' => { myzip => { file => Mojo::Asset::Memory->new->add_chunk('lalala'), filename => 'foo.zip', DNT => 1 } }); my $tx = $t->form('http://kraih.com' => 'UTF-8' => {a => 'b'}); my $tx = $t->form('http://kraih.com' => {a => 'b'} => {DNT => 1}); my $tx = $t->form('http://kraih.com', 'UTF-8', {a => 'b'}, {DNT => 1});
Versatile Mojo::Transaction::HTTP builder for POST requests with form data.
POST
# Multipart upload with filename my $tx = $t->form( 'mojolicio.us' => {fun => {content => 'Hello!', filename => 'test.txt'}}); # Multipart upload streamed from file my $tx = $t->form('mojolicio.us' => {fun => {file => '/etc/passwd'}});
While the "multipart/form-data" content type will be automatically used instead of "application/x-www-form-urlencoded" when necessary, you can also enforce it by setting the header manually.
# Force multipart my $tx = $t->form( 'http://kraih.com/foo', {a => 'b'}, {'Content-Type' => 'multipart/form-data'} );
json
my $tx = $t->json('kraih.com' => {a => 'b'}); my $tx = $t->json('http://kraih.com' => [1, 2, 3]); my $tx = $t->json('http://kraih.com' => {a => 'b'} => {DNT => 1}); my $tx = $t->json('http://kraih.com' => [1, 2, 3] => {DNT => 1});
Versatile Mojo::Transaction::HTTP builder for POST requests with JSON data.
# Change method my $tx = $t->json('mojolicio.us/hello', {hello => 'world'}); $tx->req->method('PATCH');
peer
my ($scheme, $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 followup request for 301, 302, 303, 307 or 308 redirect response if possible.
301
302
303
307
308
tx
my $tx = $t->tx(GET => 'kraih.com'); my $tx = $t->tx(POST => 'http://kraih.com'); my $tx = $t->tx(GET => 'http://kraih.com' => {DNT => 1}); my $tx = $t->tx(PUT => 'http://kraih.com' => 'Hi!'); my $tx = $t->tx(POST => 'http://kraih.com' => {DNT => 1} => 'Hi!');
Versatile general purpose Mojo::Transaction::HTTP builder for requests.
# Inspect generated request say $t->tx(GET => 'mojolicio.us' => {DNT => 1} => 'Bye!')->req->to_string; # Streaming response my $tx = $t->tx(GET => 'http://mojolicio.us'); $tx->res->body(sub { say $_[1] }); # Custom socket my $tx = $t->tx(GET => 'http://mojolicio.us'); $tx->connection($sock);
websocket
my $tx = $t->websocket('ws://localhost:3000'); my $tx = $t->websocket('ws://localhost:3000' => {DNT => 1});
Versatile Mojo::Transaction::WebSocket builder for WebSocket handshake requests.
Mojolicious, Mojolicious::Guides, http://mojolicio.us.
To install Mojolicious, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious
CPAN shell
perl -MCPAN -e shell install Mojolicious
For more information on module installation, please visit the detailed CPAN module installation guide.