1Mojo::Transaction::HTTPU(s3e)r Contributed Perl DocumentaMtoijoon::Transaction::HTTP(3)
2
3
4
6 Mojo::Transaction::HTTP - HTTP transaction
7
9 use Mojo::Transaction::HTTP;
10
11 # Client
12 my $tx = Mojo::Transaction::HTTP->new;
13 $tx->req->method('GET');
14 $tx->req->url->parse('http://example.com');
15 $tx->req->headers->accept('application/json');
16 say $tx->res->code;
17 say $tx->res->headers->content_type;
18 say $tx->res->body;
19 say $tx->remote_address;
20
21 # Server
22 my $tx = Mojo::Transaction::HTTP->new;
23 say $tx->req->method;
24 say $tx->req->url->to_abs;
25 say $tx->req->headers->accept;
26 say $tx->remote_address;
27 $tx->res->code(200);
28 $tx->res->headers->content_type('text/plain');
29 $tx->res->body('Hello World!');
30
32 Mojo::Transaction::HTTP is a container for HTTP transactions, based on
33 RFC 7230 <http://tools.ietf.org/html/rfc7230> and RFC 7231
34 <http://tools.ietf.org/html/rfc7231>.
35
37 Mojo::Transaction::HTTP inherits all events from Mojo::Transaction and
38 can emit the following new ones.
39
40 request
41 $tx->on(request => sub {
42 my $tx = shift;
43 ...
44 });
45
46 Emitted when a request is ready and needs to be handled.
47
48 $tx->on(request => sub {
49 my $tx = shift;
50 $tx->res->headers->header('X-Bender' => 'Bite my shiny metal ass!');
51 });
52
53 resume
54 $tx->on(resume => sub {
55 my $tx = shift;
56 ...
57 });
58
59 Emitted when transaction is resumed.
60
61 unexpected
62 $tx->on(unexpected => sub {
63 my ($tx, $res) = @_;
64 ...
65 });
66
67 Emitted for unexpected "1xx" responses that will be ignored.
68
69 $tx->on(unexpected => sub {
70 my $tx = shift;
71 $tx->res->on(finish => sub { say 'Follow-up response is finished.' });
72 });
73
75 Mojo::Transaction::HTTP inherits all attributes from Mojo::Transaction
76 and implements the following new ones.
77
78 previous
79 my $previous = $tx->previous;
80 $tx = $tx->previous(Mojo::Transaction::HTTP->new);
81
82 Previous transaction that triggered this follow-up transaction, usually
83 a Mojo::Transaction::HTTP object.
84
85 # Paths of previous requests
86 say $tx->previous->previous->req->url->path;
87 say $tx->previous->req->url->path;
88
90 Mojo::Transaction::HTTP inherits all methods from Mojo::Transaction and
91 implements the following new ones.
92
93 client_read
94 $tx->client_read($bytes);
95
96 Read data client-side, used to implement user agents such as
97 Mojo::UserAgent.
98
99 client_write
100 my $bytes = $tx->client_write;
101
102 Write data client-side, used to implement user agents such as
103 Mojo::UserAgent.
104
105 is_empty
106 my $bool = $tx->is_empty;
107
108 Check transaction for "HEAD" request and "1xx", 204 or 304 response.
109
110 keep_alive
111 my $bool = $tx->keep_alive;
112
113 Check if connection can be kept alive.
114
115 redirects
116 my $redirects = $tx->redirects;
117
118 Return an array reference with all previous transactions that preceded
119 this follow-up transaction.
120
121 # Paths of all previous requests
122 say $_->req->url->path for @{$tx->redirects};
123
124 resume
125 $tx = $tx->resume;
126
127 Resume transaction.
128
129 server_read
130 $tx->server_read($bytes);
131
132 Read data server-side, used to implement web servers such as
133 Mojo::Server::Daemon.
134
135 server_write
136 my $bytes = $tx->server_write;
137
138 Write data server-side, used to implement web servers such as
139 Mojo::Server::Daemon.
140
142 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
143
144
145
146perl v5.32.0 2020-07-28 Mojo::Transaction::HTTP(3)