?
avatar tassosm
tassosm
22 May 2018

Steps to reproduce the issue

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;

Expected result

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.

Actual result

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":....

System information (as much as possible)

PHP 7.1.11-1+ubuntu14.04.1+deb.sury.org+1
Joomla! 4.0.0-alpha4-dev Development

Additional comments

avatar tassosm tassosm - open - 22 May 2018
avatar joomla-cms-bot joomla-cms-bot - change - 22 May 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 22 May 2018
avatar tassosm tassosm - change - 22 May 2018
The description was changed
avatar tassosm tassosm - edited - 22 May 2018
avatar tassosm tassosm - change - 22 May 2018
Title
Http\Response different' body anymore
Http\Response doesn't include the body information
avatar tassosm tassosm - edited - 22 May 2018
avatar tassosm tassosm - change - 22 May 2018
The description was changed
avatar tassosm tassosm - edited - 22 May 2018
avatar PhilETaylor PhilETaylor - comment - 22 May 2018
avatar franz-wohlkoenig franz-wohlkoenig - change - 22 May 2018
Title
Http\Response doesn't include the body information
[4.0] Http\Response doesn't include the body information
avatar joomla-cms-bot joomla-cms-bot - edited - 22 May 2018
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 22 May 2018

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.

avatar franz-wohlkoenig franz-wohlkoenig - change - 22 May 2018
Category Administration
avatar PhilETaylor PhilETaylor - comment - 22 May 2018
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 22 May 2018

@PhilETaylor so the reported Issue is not about 4.0?

avatar mbabker
mbabker - comment - 22 May 2018

#16538 changed the HttpFactory in 4.0 to default set a User-Agent. So if you expected an error because of that, it no longer happens in 4.0.

So, what you're seeing is as expected.

avatar mbabker mbabker - change - 22 May 2018
Status New Closed
Closed_Date 0000-00-00 00:00:00 2018-05-22 17:37:00
Closed_By mbabker
avatar mbabker mbabker - close - 22 May 2018
avatar PhilETaylor PhilETaylor - comment - 22 May 2018
avatar PhilETaylor PhilETaylor - comment - 22 May 2018
avatar tassosm
tassosm - comment - 22 May 2018

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.

avatar PhilETaylor PhilETaylor - comment - 22 May 2018
avatar mbabker
mbabker - comment - 22 May 2018

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).

avatar PhilETaylor PhilETaylor - comment - 22 May 2018
avatar PhilETaylor
PhilETaylor - comment - 22 May 2018

This would give false - I think this is what you are saying you are doing.

var_dump(isset($response->body));

for the reasons @mbabker state

avatar mbabker
mbabker - comment - 22 May 2018

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.

avatar tassosm
tassosm - comment - 22 May 2018

@PhilETaylor you're right. I was really confused in the first place.
@mbabker now it's more clear. Thanks.

Add a Comment

Login with GitHub to post a comment