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 <http://tools.ietf.org/html/rfc7230>, RFC 7231
30 <http://tools.ietf.org/html/rfc7231>, RFC 7235
31 <http://tools.ietf.org/html/rfc7235> and RFC 2817
32 <http://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 request_id
72 my $id = $req->request_id;
73 $req = $req->request_id('aee7d5d8');
74
75 Request ID, defaults to a reasonably unique value.
76
77 url
78 my $url = $req->url;
79 $req = $req->url(Mojo::URL->new);
80
81 HTTP request URL, defaults to a Mojo::URL object.
82
83 # Get request information
84 my $info = $req->url->to_abs->userinfo;
85 my $host = $req->url->to_abs->host;
86 my $path = $req->url->to_abs->path;
87
88 via_proxy
89 my $bool = $req->via_proxy;
90 $req = $req->via_proxy($bool);
91
92 Request can be performed through a proxy server.
93
95 Mojo::Message::Request inherits all methods from Mojo::Message and
96 implements the following new ones.
97
98 clone
99 my $clone = $req->clone;
100
101 Return a new Mojo::Message::Request object cloned from this request if
102 possible, otherwise return "undef".
103
104 cookies
105 my $cookies = $req->cookies;
106 $req = $req->cookies(Mojo::Cookie::Request->new);
107 $req = $req->cookies({name => 'foo', value => 'bar'});
108
109 Access request cookies, usually Mojo::Cookie::Request objects.
110
111 # Names of all cookies
112 say $_->name for @{$req->cookies};
113
114 every_param
115 my $values = $req->every_param('foo');
116
117 Similar to "param", but returns all values sharing the same name as an
118 array reference.
119
120 # Get first value
121 say $req->every_param('foo')->[0];
122
123 extract_start_line
124 my $bool = $req->extract_start_line(\$str);
125
126 Extract request-line from string.
127
128 fix_headers
129 $req = $req->fix_headers;
130
131 Make sure request has all required headers.
132
133 get_start_line_chunk
134 my $bytes = $req->get_start_line_chunk($offset);
135
136 Get a chunk of request-line data starting from a specific position.
137 Note that this method finalizes the request.
138
139 is_handshake
140 my $bool = $req->is_handshake;
141
142 Check "Upgrade" header for "websocket" value.
143
144 is_secure
145 my $bool = $req->is_secure;
146
147 Check if connection is secure.
148
149 is_xhr
150 my $bool = $req->is_xhr;
151
152 Check "X-Requested-With" header for "XMLHttpRequest" value.
153
154 param
155 my $value = $req->param('foo');
156
157 Access "GET" and "POST" parameters extracted from the query string and
158 "application/x-www-form-urlencoded" or "multipart/form-data" message
159 body. If there are multiple values sharing the same name, and you want
160 to access more than just the last one, you can use "every_param". Note
161 that this method caches all data, so it should not be called before the
162 entire request body has been received. Parts of the request body need
163 to be loaded into memory to parse "POST" parameters, so you have to
164 make sure it is not excessively large. There's a 16MiB limit for
165 requests by default.
166
167 params
168 my $params = $req->params;
169
170 All "GET" and "POST" parameters extracted from the query string and
171 "application/x-www-form-urlencoded" or "multipart/form-data" message
172 body, usually a Mojo::Parameters object. Note that this method caches
173 all data, so it should not be called before the entire request body has
174 been received. Parts of the request body need to be loaded into memory
175 to parse "POST" parameters, so you have to make sure it is not
176 excessively large. There's a 16MiB limit for requests by default.
177
178 # Get parameter names and values
179 my $hash = $req->params->to_hash;
180
181 parse
182 $req = $req->parse('GET /foo/bar HTTP/1.1');
183 $req = $req->parse({PATH_INFO => '/'});
184
185 Parse HTTP request chunks or environment hash.
186
187 query_params
188 my $params = $req->query_params;
189
190 All "GET" parameters, usually a Mojo::Parameters object.
191
192 # Turn GET parameters to hash and extract value
193 say $req->query_params->to_hash->{foo};
194
195 start_line_size
196 my $size = $req->start_line_size;
197
198 Size of the request-line in bytes. Note that this method finalizes the
199 request.
200
202 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
203
204
205
206perl v5.32.0 2020-07-28 Mojo::Message::Request(3)