retailcrm / api-client-php

PHP client for RetailCRM API
http://www.retailcrm.ru
MIT License
60 stars 58 forks source link

Uncaught Cannot deserialize body: Failed to JSON decode data. This is not supposed to happen #128

Closed IoakkunShoik closed 2 years ago

IoakkunShoik commented 2 years ago

PHP Fatal error: Uncaught Cannot deserialize body: Failed to JSON decode data. This is not supposed to happen. (0)

0 /data/projects/users_www/iosh/myProj/vendor/retailcrm/api-client-php/src/Handler/Response/UnmarshalResponseHandler.php(28): RetailCrm\Api\Handler\Response\AbstractResponseHandler->unmarshalBody()

1 /data/projects/users_www/iosh/myProj/vendor/retailcrm/api-client-php/src/Handler/Response/AbstractResponseHandler.php(68): RetailCrm\Api\Handler\Response\UnmarshalResponseHandler->handleResponse()

2 /data/projects/users_www/iosh/myProj/vendor/retailcrm/api-client-php/src/Handler/AbstractHandler.php(35): RetailCrm\Api\Handler\Response\AbstractResponseHandler->handle()

3 /data/projects/users_www/iosh/myProj/vendor/retailcrm/api-client-php/src/Handler/Response/AbstractResponseHandler.php(84): RetailCrm\Api\Handler\AbstractHandler->handle()

4 /data/projects/users_www/iosh/myProj/vendor/retailcrm/api-client-php/src/Handler/Response/FilesDownloadResponseHandler.php(30): RetailCrm\Api in /data/projects/users_www/iosh/myProj/vendor/retailcrm/api-client-php/src/Handler/Response/AbstractResponseHandler.php on line 99

Neur0toxine commented 2 years ago

Looks like the client cannot deserialize the response. As the issue title states, this is not supposed to happen because the client has a separate handler for the /api/v5/files/{id}/download route. For some reason, it was not called, and the default handler was used, which ultimately lead to the deserialization attempt.

That's unfortunate.

If you're using a custom response pipeline - try to initialize the client with the default one. If it still fails with the same issue or you are using the default pipeline (for example, that would be the case with SimpleClientFactory), or you don't know about the response pipeline at all, then try to initialize the client using the code below:

use RetailCrm\Api\Builder\ClientBuilder;
use RetailCrm\Api\Builder\FormEncoderBuilder;
use RetailCrm\Api\Handler\Request\HeaderAuthenticatorHandler;
use Psr\Log\AbstractLogger;

$client = (new ClientBuilder())
    ->setApiUrl('system URL')
    ->setAuthenticatorHandler(new HeaderAuthenticatorHandler('system API key'))
    ->setFormEncoder((new FormEncoderBuilder())->build())
    ->setDebugLogger(new class extends AbstractLogger {
        public function log($level, $message, array $context = [])
        {
            printf('[%s] %s %s' . PHP_EOL, $level, $message, json_encode($context));
        }
    })
    ->build();

There is no need for additional logging calls - this debug logger will do it for you. Please, run your code with this client and send us the output (by the way, it'll be better to omit any private information from the data). With this information, we'll be able to understand the issue better and therefore help you with it.