1Mojo::Message::Request(U3s)er Contributed Perl DocumentatMioojno::Message::Request(3)
2
3
4
6 Mojo::Message::Request - HTTP request
7
9 use Mojo::Message::Request;
10
11 # Parse
12 my $req = Mojo::Message::Request->new;
13 $req->parse("GET /foo HTTP/1.0\x0d\x0a");
14 $req->parse("Content-Length: 12\x0d\x0a");
15 $req->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
16 $req->parse('Hello World!');
17 say $req->method;
18 say $req->headers->content_type;
19 say $req->body;
20
21 # Build
22 my $req = Mojo::Message::Request->new;
23 $req->url->parse('http://127.0.0.1/foo/bar');
24 $req->method('GET');
25 say $req->to_string;
26
28 Mojo::Message::Request is a container for HTTP requests, based on RFC
29 7230 <https://tools.ietf.org/html/rfc7230>, RFC 7231
30 <https://tools.ietf.org/html/rfc7231>, RFC 7235
31 <https://tools.ietf.org/html/rfc7235> and RFC 2817
32 <https://tools.ietf.org/html/rfc2817>.
33
35 Mojo::Message::Request inherits all events from Mojo::Message.
36
38 Mojo::Message::Request inherits all attributes from Mojo::Message and
39 implements the following new ones.
40
41 env
42 my $env = $req->env;
43 $req = $req->env({PATH_INFO => '/'});
44
45 Direct access to the "CGI" or "PSGI" environment hash if available.
46
47 # Check CGI version
48 my $version = $req->env->{GATEWAY_INTERFACE};
49
50 # Check PSGI version
51 my $version = $req->env->{'psgi.version'};
52
53 method
54 my $method = $req->method;
55 $req = $req->method('POST');
56
57 HTTP request method, defaults to "GET".
58
59 proxy
60 my $url = $req->proxy;
61 $req = $req->proxy(Mojo::URL->new('http://127.0.0.1:3000'));
62
63 Proxy URL for request.
64
65 reverse_proxy
66 my $bool = $req->reverse_proxy;
67 $req = $req->reverse_proxy($bool);
68
69 Request has been performed through a reverse proxy.
70
71 trusted_proxies
72 my $proxies = $req->trusted_proxies;
73 $req = $req->trusted_proxies(['10.0/8', '127.0.0.1', '172.16.0/12', '192.168.0/16', 'fc00::/7']);
74
75 Trusted reverse proxies, addresses or networks in CIDR form.
76
77 request_id
78 my $id = $req->request_id;
79 $req = $req->request_id('aee7d5d8');
80
81 Request ID, defaults to a reasonably unique value.
82
83 url
84 my $url = $req->url;
85 $req = $req->url(Mojo::URL->new);
86
87 HTTP request URL, defaults to a Mojo::URL object.
88
89 # Get request information
90 my $info = $req->url->to_abs->userinfo;
91 my $host = $req->url->to_abs->host;
92 my $path = $req->url->to_abs->path;
93
94 via_proxy
95 my $bool = $req->via_proxy;
96 $req = $req->via_proxy($bool);
97
98 Request can be performed through a proxy server.
99
101 Mojo::Message::Request inherits all methods from Mojo::Message and
102 implements the following new ones.
103
104 clone
105 my $clone = $req->clone;
106
107 Return a new Mojo::Message::Request object cloned from this request if
108 possible, otherwise return "undef".
109
110 cookies
111 my $cookies = $req->cookies;
112 $req = $req->cookies(Mojo::Cookie::Request->new);
113 $req = $req->cookies({name => 'foo', value => 'bar'});
114
115 Access request cookies, usually Mojo::Cookie::Request objects.
116
117 # Names of all cookies
118 say $_->name for @{$req->cookies};
119
120 every_param
121 my $values = $req->every_param('foo');
122
123 Similar to "param", but returns all values sharing the same name as an
124 array reference.
125
126 # Get first value
127 say $req->every_param('foo')->[0];
128
129 extract_start_line
130 my $bool = $req->extract_start_line(\$str);
131
132 Extract request-line from string.
133
134 fix_headers
135 $req = $req->fix_headers;
136
137 Make sure request has all required headers.
138
139 get_start_line_chunk
140 my $bytes = $req->get_start_line_chunk($offset);
141
142 Get a chunk of request-line data starting from a specific position.
143 Note that this method finalizes the request.
144
145 is_handshake
146 my $bool = $req->is_handshake;
147
148 Check "Upgrade" header for "websocket" value.
149
150 is_secure
151 my $bool = $req->is_secure;
152
153 Check if connection is secure.
154
155 is_xhr
156 my $bool = $req->is_xhr;
157
158 Check "X-Requested-With" header for "XMLHttpRequest" value.
159
160 param
161 my $value = $req->param('foo');
162
163 Access "GET" and "POST" parameters extracted from the query string and
164 "application/x-www-form-urlencoded" or "multipart/form-data" message
165 body. If there are multiple values sharing the same name, and you want
166 to access more than just the last one, you can use "every_param". Note
167 that this method caches all data, so it should not be called before the
168 entire request body has been received. Parts of the request body need
169 to be loaded into memory to parse "POST" parameters, so you have to
170 make sure it is not excessively large. There's a 16MiB limit for
171 requests by default.
172
173 params
174 my $params = $req->params;
175
176 All "GET" and "POST" parameters extracted from the query string and
177 "application/x-www-form-urlencoded" or "multipart/form-data" message
178 body, usually a Mojo::Parameters object. Note that this method caches
179 all data, so it should not be called before the entire request body has
180 been received. Parts of the request body need to be loaded into memory
181 to parse "POST" parameters, so you have to make sure it is not
182 excessively large. There's a 16MiB limit for requests by default.
183
184 # Get parameter names and values
185 my $hash = $req->params->to_hash;
186
187 parse
188 $req = $req->parse('GET /foo/bar HTTP/1.1');
189 $req = $req->parse({PATH_INFO => '/'});
190
191 Parse HTTP request chunks or environment hash.
192
193 query_params
194 my $params = $req->query_params;
195
196 All "GET" parameters, usually a Mojo::Parameters object.
197
198 # Turn GET parameters to hash and extract value
199 say $req->query_params->to_hash->{foo};
200
201 start_line_size
202 my $size = $req->start_line_size;
203
204 Size of the request-line in bytes. Note that this method finalizes the
205 request.
206
208 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
209
210
211
212perl v5.32.1 2021-02-07 Mojo::Message::Request(3)