commercetools-sdk-php-v2 icon indicating copy to clipboard operation
commercetools-sdk-php-v2 copied to clipboard

Allow getting raw results of an API Response

Open MurzNN opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe. I need to get the full raw results of an API response. In v1 it was possible via function $response->toArray():

$response = $request->execute();
$responseData = $response->toArray();

But in v2 this method is marked as protected in lib/commercetools-base/src/Base/JsonObjectModel.php:

    final protected function toArray(): array

So we can't get the raw results by this method.

Describe the solution you'd like Is there any other way to get the raw response as an array, like in v1? If not, could you please consider making this method public in v2, like in v1?

Describe alternatives you've considered Alternatively, maybe it's good to provide another method to get raw results if you don't want to make the current toArray public for some reason?

MurzNN avatar Jun 14 '24 15:06 MurzNN

Seems we can use the jsonSerialize() method to get the raw data of the response, is it right?

$response = $request->execute();
$responseData = (array) $response->jsonSerialize();

MurzNN avatar Jun 18 '24 06:06 MurzNN

This would still deserialize the response using json_deserialize. The jsonSerialize method would then convert the result to a serializable representation.

You could save the step by just using $request->send() as it would return the response with the raw string in the body.

jenschude avatar Jun 18 '24 11:06 jenschude