diff --git a/src/Client.php b/src/Client.php index 4228b73e..b6eb8b7b 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,24 @@ 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 + * + */ + protected function isJsonResponse(Response $response) + { + foreach ($response->getHeader('Content-Type') as $type) { + if (str_starts_with($type, 'application/json')) { + return true; + } + } + return false; + } + /** * Get the access token or redirect to the authentication URL. * @@ -114,7 +133,7 @@ public function authenticate() ); } - if (in_array('application/json', $response->getHeader('Content-Type'))) { + if ($this->isJsonResponse($response)) { $token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]); } else { parse_str((string) $response->getBody(), $token); @@ -388,7 +407,8 @@ public function refreshToken($token = null) ); } - if (in_array('application/json', $response->getHeader('Content-Type'))) { + + if ($this->isJsonResponse($response)) { $token = array_merge(json_decode((string) $response->getBody(), true), ['created' => time()]); } else { parse_str((string) $response->getBody(), $token);