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