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!!!
1
  • I get the same. I tried $client->getResponse() also, but that is not allowed. Commented Apr 21, 2021 at 10:44

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

If you are using Client::createChromeClient() you can use the following code

$statusCode = $client->executeScript('return window.performance.getEntries()[0].responseStatus');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.