1Mojo::UserAgent::TransaUcsteorr(C3o)ntributed Perl DocumMeonjtoa:t:iUosnerAgent::Transactor(3)
2
3
4

NAME

6       Mojo::UserAgent::Transactor - User agent transactor
7

SYNOPSIS

9         use Mojo::UserAgent::Transactor;
10
11         # GET request with Accept header
12         my $t = Mojo::UserAgent::Transactor->new;
13         say $t->tx(GET => 'http://example.com' => {Accept => '*/*'})->req->to_string;
14
15         # POST request with form-data
16         say $t->tx(POST => 'example.com' => form => {a => 'b'})->req->to_string;
17
18         # PUT request with JSON data
19         say $t->tx(PUT => 'example.com' => json => {a => 'b'})->req->to_string;
20

DESCRIPTION

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

GENERATORS

26       These content generators are available by default.
27
28   form
29         $t->tx(POST => 'http://example.com' => form => {a => 'b'});
30
31       Generate query string, "application/x-www-form-urlencoded" or
32       "multipart/form-data" content. See "tx" for more.
33
34   json
35         $t->tx(PATCH => 'http://example.com' => json => {a => 'b'});
36
37       Generate JSON content with Mojo::JSON. See "tx" for more.
38
39   multipart
40         $t->tx(PUT => 'http://example.com' => multipart => ['Hello', 'World!']);
41
42       Generate multipart content. See "tx" for more.
43

ATTRIBUTES

45       Mojo::UserAgent::Transactor implements the following attributes.
46
47   compressed
48         my $bool = $t->compressed;
49         $t       = $t->compressed($bool);
50
51       Try to negotiate compression for the response content and decompress it
52       automatically, defaults to the value of the "MOJO_GZIP" environment
53       variable or true.
54
55   generators
56         my $generators = $t->generators;
57         $t             = $t->generators({foo => sub {...}});
58
59       Registered content generators, by default only "form", "json" and
60       "multipart" are already defined.
61
62   name
63         my $name = $t->name;
64         $t       = $t->name('Mojolicious');
65
66       Value for "User-Agent" request header of generated transactions,
67       defaults to "Mojolicious (Perl)".
68

METHODS

70       Mojo::UserAgent::Transactor inherits all methods from Mojo::Base and
71       implements the following new ones.
72
73   add_generator
74         $t = $t->add_generator(foo => sub {...});
75
76       Register a content generator.
77
78         $t->add_generator(foo => sub ($t, $tx, @args) {...});
79
80   endpoint
81         my ($proto, $host, $port) = $t->endpoint(Mojo::Transaction::HTTP->new);
82
83       Actual endpoint for transaction.
84
85   peer
86         my ($proto, $host, $port) = $t->peer(Mojo::Transaction::HTTP->new);
87
88       Actual peer for transaction.
89
90   promisify
91         $t->promisify(Mojo::Promise->new, Mojo::Transaction::HTTP->new);
92
93       Resolve or reject Mojo::Promise object with Mojo::Transaction::HTTP
94       object.
95
96   proxy_connect
97         my $tx = $t->proxy_connect(Mojo::Transaction::HTTP->new);
98
99       Build Mojo::Transaction::HTTP proxy "CONNECT" request for transaction
100       if possible.
101
102   redirect
103         my $tx = $t->redirect(Mojo::Transaction::HTTP->new);
104
105       Build Mojo::Transaction::HTTP follow-up request for 301, 302, 303, 307
106       or 308 redirect response if possible.
107
108   tx
109         my $tx = $t->tx(GET  => 'example.com');
110         my $tx = $t->tx(POST => 'http://example.com');
111         my $tx = $t->tx(GET  => 'http://example.com' => {Accept => '*/*'});
112         my $tx = $t->tx(PUT  => 'http://example.com' => 'Content!');
113         my $tx = $t->tx(PUT  => 'http://example.com' => form => {a => 'b'});
114         my $tx = $t->tx(PUT  => 'http://example.com' => json => {a => 'b'});
115         my $tx = $t->tx(PUT  => 'https://example.com' => multipart => ['a', 'b']);
116         my $tx = $t->tx(POST => 'example.com' => {Accept => '*/*'} => 'Content!');
117         my $tx = $t->tx(PUT => 'example.com' => {Accept => '*/*'} => form => {a => 'b'});
118         my $tx = $t->tx(PUT => 'example.com' => {Accept => '*/*'} => json => {a => 'b'});
119         my $tx = $t->tx(PUT => 'example.com' => {Accept => '*/*'} => multipart => ['a', 'b']);
120
121       Versatile general purpose Mojo::Transaction::HTTP transaction builder
122       for requests, with support for "GENERATORS".
123
124         # Generate and inspect custom GET request with DNT header and content
125         say $t->tx(GET => 'example.com' => {DNT => 1} => 'Bye!')->req->to_string;
126
127         # Stream response content to STDOUT
128         my $tx = $t->tx(GET => 'http://example.com');
129         $tx->res->content->unsubscribe('read')->on(read => sub { say $_[1] });
130
131         # PUT request with content streamed from file
132         my $tx = $t->tx(PUT => 'http://example.com');
133         $tx->req->content->asset(Mojo::Asset::File->new(path => '/foo.txt'));
134
135       The "json" content generator uses Mojo::JSON for encoding and sets the
136       content type to "application/json".
137
138         # POST request with "application/json" content
139         my $tx = $t->tx(POST => 'http://example.com' => json => {a => 'b', c => [1, 2, 3]});
140
141       The "form" content generator will automatically use query parameters
142       for "GET" and "HEAD" requests.
143
144         # GET request with query parameters
145         my $tx = $t->tx(GET => 'http://example.com' => form => {a => 'b'});
146
147       For all other request methods the "application/x-www-form-urlencoded"
148       content type is used.
149
150         # POST request with "application/x-www-form-urlencoded" content
151         my $tx = $t->tx(POST => 'http://example.com' => form => {a => 'b', c => 'd'});
152
153       Parameters may be encoded with the "charset" option.
154
155         # PUT request with Shift_JIS encoded form values
156         my $tx = $t->tx(PUT => 'example.com' => form => {a => 'b'} => charset => 'Shift_JIS');
157
158       An array reference can be used for multiple form values sharing the
159       same name.
160
161         # POST request with form values sharing the same name
162         my $tx = $t->tx(POST => 'http://example.com' => form => {a => ['b', 'c', 'd']});
163
164       A hash reference with a "content" or "file" value can be used to switch
165       to the "multipart/form-data" content type for file uploads.
166
167         # POST request with "multipart/form-data" content
168         my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => {content => 'lala'}});
169
170         # POST request with multiple files sharing the same name
171         my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => [{content => 'first'}, {content => 'second'}]});
172
173       The "file" value should contain the path to the file you want to upload
174       or an asset object, like Mojo::Asset::File or Mojo::Asset::Memory.
175
176         # POST request with upload streamed from file
177         my $tx = $t->tx(POST => 'http://example.com' => form => {mytext => {file => '/foo.txt'}});
178
179         # POST request with upload streamed from asset
180         my $asset = Mojo::Asset::Memory->new->add_chunk('lalala');
181         my $tx    = $t->tx(POST => 'http://example.com' => form => {mytext => {file => $asset}});
182
183       A "filename" value will be generated automatically, but can also be set
184       manually if necessary. All remaining values in the hash reference get
185       merged into the "multipart/form-data" content as headers.
186
187         # POST request with form values and customized upload (filename and header)
188         my $tx = $t->tx(POST => 'http://example.com' => form => {
189           a      => 'b',
190           c      => 'd',
191           mytext => {
192             content        => 'lalala',
193             filename       => 'foo.txt',
194             'Content-Type' => 'text/plain'
195           }
196         });
197
198       The "multipart/form-data" content type can also be enforced by setting
199       the "Content-Type" header manually.
200
201         # Force "multipart/form-data"
202         my $headers = {'Content-Type' => 'multipart/form-data'};
203         my $tx = $t->tx(POST => 'example.com' => $headers => form => {a => 'b'});
204
205       The "multipart" content generator can be used to build custom multipart
206       requests and does not set a content type.
207
208         # POST request with multipart content ("foo" and "bar")
209         my $tx = $t->tx(POST => 'http://example.com' => multipart => ['foo', 'bar']);
210
211       Similar to the "form" content generator you can also pass hash
212       references with "content" or "file" values, as well as headers.
213
214         # POST request with multipart content streamed from file
215         my $tx = $t->tx(POST => 'http://example.com' => multipart => [{file => '/foo.txt'}]);
216
217         # PUT request with multipart content streamed from asset
218         my $headers = {'Content-Type' => 'multipart/custom'};
219         my $asset   = Mojo::Asset::Memory->new->add_chunk('lalala');
220         my $tx      = $t->tx(PUT => 'http://example.com' => $headers => multipart => [{file => $asset}]);
221
222         # POST request with multipart content and custom headers
223         my $tx = $t->tx(POST => 'http://example.com' => multipart => [
224           {
225             content            => 'Hello',
226             'Content-Type'     => 'text/plain',
227             'Content-Language' => 'en-US'
228           },
229           {
230             content            => 'World!',
231             'Content-Type'     => 'text/plain',
232             'Content-Language' => 'en-US'
233           }
234         ]);
235
236   upgrade
237         my $tx = $t->upgrade(Mojo::Transaction::HTTP->new);
238
239       Build Mojo::Transaction::WebSocket follow-up transaction for WebSocket
240       handshake if possible.
241
242   websocket
243         my $tx = $t->websocket('ws://example.com');
244         my $tx = $t->websocket('ws://example.com' => {DNT => 1} => ['v1.proto']);
245
246       Versatile Mojo::Transaction::HTTP transaction builder for WebSocket
247       handshake requests.
248

SEE ALSO

250       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
251
252
253
254perl v5.32.1                      2021-02-07    Mojo::UserAgent::Transactor(3)
Impressum