diff --git a/src/Client.php b/src/Client.php index 74153081..93916786 100644 --- a/src/Client.php +++ b/src/Client.php @@ -13,6 +13,7 @@ use Joomla\Http\Exception\UnexpectedResponseException; use Joomla\Http\Http; use Joomla\Http\HttpFactory; +use Joomla\Http\Response; use Joomla\Input\Input; use Joomla\Uri\Uri; @@ -79,6 +80,28 @@ public function __construct($options = [], ?Http $http = null, ?Input $input = n $this->application = $application; } + /** + * Tests if given response contains JSON header + * + * @param Response $response The response object + * + * @return boolean + * + */ + private static function isJsonResponse(Response $response) + { + foreach (['Content-Type', 'content-type'] as $type) { + if (array_key_exists($type, $response->headers)) { + $content_type = is_array($response->headers[$type]) ? + $response->headers[$type][0] : $response->headers[$type]; + if (strpos($content_type, 'application/json') !== false) { + return true; + } + } + } + return false; + } + /** * Get the access token or redirect to the authentication URL. * @@ -112,7 +135,7 @@ public function authenticate() ); } - if (in_array('application/json', $response->getHeader('Content-Type'))) { + if ($this->isJsonResponse($response)) { $token = array_merge(json_decode($response->body, true), ['created' => time()]); } else { parse_str($response->body, $token);