@@ -161,6 +161,32 @@ provides access to the form properties (e.g. ``$form->getUri()``,
161
161
// submit that form
162
162
$crawler = $client->submit($form);
163
163
164
+ Custom Header Handling
165
+ ~~~~~~~~~~~~~~~~~~~~~~
166
+
167
+ .. versionadded :: 5.2
168
+
169
+ The ``getHeaders() `` method was introduced in Symfony 5.2.
170
+
171
+ The optional HTTP headers passed to the ``request() `` method follows the FastCGI
172
+ request format (uppercase, underscores instead of dashes and prefixed with ``HTTP_ ``).
173
+ Before saving those headers to the request, they are lower-cased, with ``HTTP_ ``
174
+ stripped, and underscores turned to dashes.
175
+
176
+ If you're making a request to an application that has special rules about header
177
+ capitalization or punctuation, override the ``getHeaders() `` method, which must
178
+ return an associative array of headers::
179
+
180
+ protected function getHeaders(Request $request): array
181
+ {
182
+ $headers = parent::getHeaders($request);
183
+ if (isset($request->getServer()['api_key'])) {
184
+ $headers['api_key'] = $request->getServer()['api_key'];
185
+ }
186
+
187
+ return $headers;
188
+ }
189
+
164
190
Cookies
165
191
-------
166
192
@@ -310,30 +336,6 @@ dedicated web crawler or scraper such as `Goutte`_::
310
336
$openPullRequests = trim($browser->clickLink('Pull requests')->filter(
311
337
'.table-list-header-toggle a:nth-child(1)'
312
338
)->text());
313
-
314
- Dealing with Headers
315
- ~~~~~~~~~~~~~~~~~~~~
316
-
317
- The fifth parameter of `request() ` accepts an array of headers in the same format
318
- you'd see in a FastCGI request: all-upper-case, dashes replaced with underscores,
319
- prefixed with `HTTP_ `. Array keys are lower-cased, with `HTTP_ ` stripped, and
320
- underscores turned to dashes, before saving those headers to the request.
321
-
322
- If you're making a request to an application that has special rules about header
323
- capitalization or punctuation, you'll want to override HttpBrowser's `getHeaders() `
324
- method, which takes a Request object and returns an asociative array of headers.
325
- For example::
326
-
327
- protected function getHeaders(Request $request): array
328
- {
329
- $headers = parent::getHeaders($request);
330
- if (isset($request->getServer()['api_key'])) {
331
- $headers['api_key'] = $request->getServer()['api_key'];
332
- }
333
- return $headers;
334
- }
335
-
336
- This override is available as of Symfony 5.2.
337
339
338
340
Learn more
339
341
----------
0 commit comments