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 <https://tools.ietf.org/html/rfc7230> and RFC 7231
34 <https://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 ($tx) {...});
42
43 Emitted when a request is ready and needs to be handled.
44
45 $tx->on(request => sub ($tx) { $tx->res->headers->header('X-Bender' => 'Bite my shiny metal ass!') });
46
47 resume
48 $tx->on(resume => sub ($tx) {...});
49
50 Emitted when transaction is resumed.
51
52 unexpected
53 $tx->on(unexpected => sub ($tx, $res) {...});
54
55 Emitted for unexpected "1xx" responses that will be ignored.
56
57 $tx->on(unexpected => sub ($tx) { $tx->res->on(finish => sub { say 'Follow-up response is finished.' }) });
58
60 Mojo::Transaction::HTTP inherits all attributes from Mojo::Transaction
61 and implements the following new ones.
62
63 previous
64 my $previous = $tx->previous;
65 $tx = $tx->previous(Mojo::Transaction::HTTP->new);
66
67 Previous transaction that triggered this follow-up transaction, usually
68 a Mojo::Transaction::HTTP object.
69
70 # Paths of previous requests
71 say $tx->previous->previous->req->url->path;
72 say $tx->previous->req->url->path;
73
75 Mojo::Transaction::HTTP inherits all methods from Mojo::Transaction and
76 implements the following new ones.
77
78 client_read
79 $tx->client_read($bytes);
80
81 Read data client-side, used to implement user agents such as
82 Mojo::UserAgent.
83
84 client_write
85 my $bytes = $tx->client_write;
86
87 Write data client-side, used to implement user agents such as
88 Mojo::UserAgent.
89
90 is_empty
91 my $bool = $tx->is_empty;
92
93 Check transaction for "HEAD" request and "1xx", 204 or 304 response.
94
95 keep_alive
96 my $bool = $tx->keep_alive;
97
98 Check if connection can be kept alive.
99
100 redirects
101 my $redirects = $tx->redirects;
102
103 Return an array reference with all previous transactions that preceded
104 this follow-up transaction.
105
106 # Paths of all previous requests
107 say $_->req->url->path for @{$tx->redirects};
108
109 resume
110 $tx = $tx->resume;
111
112 Resume transaction.
113
114 server_read
115 $tx->server_read($bytes);
116
117 Read data server-side, used to implement web servers such as
118 Mojo::Server::Daemon.
119
120 server_write
121 my $bytes = $tx->server_write;
122
123 Write data server-side, used to implement web servers such as
124 Mojo::Server::Daemon.
125
127 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
128
129
130
131perl v5.32.1 2021-02-07 Mojo::Transaction::HTTP(3)