2

By using Symfony Panther, I sent a request and I wanted to get the response. I was able to get the body and the status code but for the header I just got an empty array.

$client = Client::createChromeClient();
$client->request('GET', 'https://google.com');
$client->getInternalResponse()->getHeaders(); // returned empty array!!!
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

2

Panther does not have access to the HTTP response as explained in this github issue https://github.com/symfony/panther/issues/17.

But if you read carefuly, you'll see that this is not a limitation of Panther but a limitation of Selenium WebDriver. See this post How to get HTTP Response Code using Selenium WebDriver.

This means that the answer to the question "Can I have access to the HTTP response code or the HTTP header using Symfony Panther?" is "No, it's not possible".

While this is not possible the workaround I found was to create an HttpClient and use it to make a request and get the response from it.

<?php

use Symfony\Component\HttpClient\HttpClient;

$client = HttpClient::create();
$response = $client->request('GET', $this->myBaseUri);
$statusCode = $response->getStatusCode();
$headers = $response->getHeaders();

Here's the documentation for HTTP Client (Symfony Docs) if you want to try this way.

Daishi
  • 12,681
  • 1
  • 19
  • 22
0

According to this issue: https://github.com/symfony/panther/issues/67, it seems that status code is not managed ( HTTP 200 will always get returned, no matter what the request actually responded.)

And same for the headers, I'm afraid. If you look at class Symfony\Component\Panther\Client and method get($url) you can see that:

$this->internalResponse = new Response($this->webDriver->getPageSource());

while Response's constructor accepts:

public function __construct(string $content = '', int $status = 200, array $headers = [])

Having these said, no matter what happens, you always get HTTP 200 and empty header array.

Lucian Filote
  • 21
  • 1
  • 4