title: Symfony 4.3 PHP: How to read the cookie from a 302 redirection response?
body: <p>What I want to do (in the controller)</p>
  
  <ol>
  <li>Receive a request, based on which</li>
  <li>Send an external HTTP request</li>
  <li>Parse the response, based on which</li>
  <li>Return a response</li>
  </ol>
  
  <p>I plan to have multiple functions that all follow this pattern. They're called one by one, in succession.</p>
  
  <p>Symfony 4.3 introduces the HttpClient component and the BrowserKit component which at first sight seem to fit the bill. But as it turns out, things get more complicated. The external HTTP requests are sent to a server that tracks sessions with cookies, which shouldn't be anything too unusual. If the request is sent without cookies, the server always responds with the same landing page content. So we need the cookie. No worries, the response to the initial request comes with a "Set-Cookie" header that contains the cookie that is sent in the "Cookie" header in the subsequent requests. The HTTP status of the response with the cookie is "302 Found" which in a normal web environment, using a web browser, redirects to the next page with the same cookie in the request. Replicating this action in my Symfony controller seems to be very tricky. Expanding on step 2. of what I want to do now:</p>
  
  <p>2.a. Send an external HTTP request</p>
  
  <p>2.b. Read the cookie from the 302 response (without automatic redirection) </p>
  
  <p>2.c. Set the "Cookie" header of the subsequent request, the value coming from 2.b.</p>
  
  <p>2.d. Send the request to the new target (set in the "Location" header of the 302 response)</p>
  
  <p>Here's some of the code from the controller:</p>
  
  <pre><code>use Symfony\Component\HttpClient\HttpClient;
  use Symfony\Component\HttpClient\Exception\RedirectionException;
  use Symfony\Component\HttpFoundation\JsonResponse;
  
  public function init(Request $request): JsonResponse
  {
      $url = "external url";
      $client = HttpClient::create();
      try {
          $response = $client-&gt;request('GET', $url, [
              'max_redirects' =&gt; -1
          ]);
      } catch (RedirectionException $redirectionException) {
  
      }
      return new JsonResponse([], 200);
  }
  </code></pre>
  
  <p>Not setting max_redirects means that the client follows the redirects and ends up returning the 200 response without any cookies. Setting the value to -1 will throw a redirection
  exception and again, the 302 response with the cookie seems to be lost.</p>
  
  <p>I've also tried the HttpBrowser component:</p>
  
  <pre><code>$cookieJar = new CookieJar();
  $client = new HttpBrowser(HttpClient::create(), null, $cookieJar);
  $client-&gt;request('GET', $url);
  </code></pre>
  
  <p>But I get a 200 response without the cookie as it automatically follows the redirects.</p>
  
  <p>I understand that the components mentioned here are new to Symfony 4.3 and "experimental" but I also don't think my task is overly complicated or unusual. But maybe there are other http clients for Symfony that would be better suited for the task?</p>