Skip to content

Add information about HttpBrowser header handling #14171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions components/browser_kit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,30 @@ dedicated web crawler or scraper such as `Goutte`_::
$openPullRequests = trim($browser->clickLink('Pull requests')->filter(
'.table-list-header-toggle a:nth-child(1)'
)->text());

Dealing with Headers
~~~~~~~~~~~~~~~~~~~~

The fifth parameter of `request()` accepts an array of headers in the same format
you'd see in a FastCGI request: all-upper-case, dashes replaced with underscores,
prefixed with `HTTP_`. Array keys are lower-cased, with `HTTP_` stripped, and
underscores turned to dashes, before saving those headers to the request.

If you're making a request to an application that has special rules about header
capitalization or punctuation, you'll want to override HttpBrowser's `getHeaders()`
method, which takes a Request object and returns an asociative array of headers.
For example::

protected function getHeaders(Request $request): array
{
$headers = parent::getHeaders($request);
if (isset($request->getServer()['api_key'])) {
$headers['api_key'] = $request->getServer()['api_key'];
}
return $headers;
}

This override is available as of Symfony 5.2.

Learn more
----------
Expand Down