From a4d71a2c82e12c17d5ab828ae5da1add67255b21 Mon Sep 17 00:00:00 2001 From: Ian Littman Date: Fri, 4 Sep 2020 01:31:46 -0500 Subject: [PATCH] Add information about HttpBrowser header handling --- components/browser_kit.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/components/browser_kit.rst b/components/browser_kit.rst index d248b7de939..ffd49aa2483 100644 --- a/components/browser_kit.rst +++ b/components/browser_kit.rst @@ -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 ----------