Skip to content

Commit a4d71a2

Browse files
iansltxjaviereguiluz
authored andcommitted
Add information about HttpBrowser header handling
1 parent 070450b commit a4d71a2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

components/browser_kit.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,30 @@ dedicated web crawler or scraper such as `Goutte`_::
310310
$openPullRequests = trim($browser->clickLink('Pull requests')->filter(
311311
'.table-list-header-toggle a:nth-child(1)'
312312
)->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.
313337

314338
Learn more
315339
----------

0 commit comments

Comments
 (0)