elastic / elasticsearch-php

Official PHP client for Elasticsearch.
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
MIT License
5.26k stars 965 forks source link

Cannot deserialize the reponse as array with Content-Type: text/plain #1218

Closed stayallive closed 2 years ago

stayallive commented 2 years ago

Summary of problem or feature request

When trying to get aliases from Elastic I get the following error:

Cannot deserialize the reponse as array with Content-Type: text/plain; charset=UTF-8
./vendor/elasticsearch/elasticsearch/src/Response/Elasticsearch.php:116 { …}
./vendor/elasticsearch/elasticsearch/src/Response/Elasticsearch.php:190 { …}

This worked on 7.x and I cannot figure out why it would fail now all of a sudden since I don't think this method is deprecated on 8.x or there is a better way to retrieve this. Probably missing something obvious so any help is appreciated!

Code snippet of problem

$aliases = $this->esClient->cat()->aliases([
    'name' => 'some-name',
]);

return $aliases[0] ?? null;

System details

ezimuel commented 2 years ago

@stayallive thanks for reporting this issue! This is actually a missing deserializer format (text/plain) in the new client :sweat:. I'm working to fix this, in the meantime you can also change the output format of the cat aliases API using 'format' => 'json' as parameter:

$aliases = $this->esClient->cat()->aliases([
    'name' => 'some-name',
    'format' => 'json'
]);

This should solve the issue. I'll update soon with the fix. Thanks!

stayallive commented 2 years ago

Thanks @ezimuel, I figured there would be a easy workaround but didn't think of the format param, will work for now 💪

ezimuel commented 2 years ago

@stayallive just sent this PR https://github.com/elastic/elasticsearch-php/pull/1220. I'll release it with 8.2.0. Let me know if you have any feedback, thanks!

stayallive commented 2 years ago

Looks good to me! 💪

ezimuel commented 2 years ago

@stayallive I'm also thinking to enable by default the Accept header to application/json format instead of text/plain for the cat() endpoints. Using this approach, we can have the same behaviour of 7.x.

stayallive commented 2 years ago

I'm not too familiar with these API's but a plain text response seems... less useful to me in almost all cases. So this sounds very logical to me.

ezimuel commented 2 years ago

@stayallive actually, I cannot change the Accept header of these endpoints since I'll change the official response from Elasticsearch. I think we will suggest to use the "format" => "json" parameter if someone needs to have array as response.

stayallive commented 2 years ago

Ah right, that makes sense... for the next version maybe 👍

Thanks again for the quick fix and workaround!