It seems that Http\Response no longer returns the body info in Joomla 4.
For instance. Let's say we want to connect to Github API without providing credentials.
$url = 'https://api.github.com';
$options = [];
$data = [];
$http = JHttpFactory::getHttp(new JRegistry($options));
$response = $http->get($url, $data);
echo $response->body;
In Joomla 3.8.7 we get the following error and therefore we know what's going wrong with the call.
Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.
However, in Joomla 4 we get the following array with no information about the error.
{"current_user_url":"https://api.github.com/user","current_user_authorizations_html_url":"https://github.com/settings/connections/applications{/client_id}","authorizations_url":"https://api.github.com/authorizations","code_search_url":....
PHP 7.1.11-1+ubuntu14.04.1+deb.sury.org+1
Joomla! 4.0.0-alpha4-dev Development
Labels |
Added:
?
|
Title |
|
Title |
|
Category | ⇒ | Administration |
@PhilETaylor so the reported Issue is not about 4.0?
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-05-22 17:37:00 |
Closed_By | ⇒ | mbabker |
OK, thanks, guys.
I am aware of the change in J4. I was confused since in my actual code I am checking whether $response->body is set and in Joomla 4 always returns false even though it's there.
There are some other minor structural tweaks since our Response object is now a subclass of https://github.com/zendframework/zend-diactoros/blob/master/src/Response.php (and inherently PSR-7 compatible), but beyond that what's in the three main attributes (headers, body, code) should still be mostly the same when going through the magic getters (the one hard B/C break is headers is now always an array).
There is no longer a $body
attribute. As of 4.0 use of $response->body
is deprecated (to be removed in Framework 3.0 and CMS 5.0) in favor of the PSR-7 ResponseInterface's API.
If it's decided this is worth patching, we'd need a __isset()
method in https://github.com/joomla-framework/http/blob/2.0-dev/src/Response.php but to be honest I don't see the value here. Generally with Response objects you should first validate the response code isn't an error (4xx or 5xx) then check if the response body is empty before continuing on; isset checks aren't efficient here.
@PhilETaylor you're right. I was really confused in the first place.
@mbabker now it's more clear. Thanks.
changed Title to make clear its about 4.0
This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/20537.