elasticsearch-php
elasticsearch-php copied to clipboard
Incorrect HTTP method for uri [/] and method [PUT] when trying to create an index
Summary of problem or feature request
I got this error when tried to create an index:
{"error":"Incorrect HTTP method for uri [/] and method [PUT], allowed: [HEAD, DELETE, GET]","status":405}
I traced it down and found out that the reason is elasticsearch host. Originally, it had a trailing slash in my settings. And it worked fine for getAlias()
for example. But failed for indices()->create()
.
The host URI needs to be sanitized probably.
Code snippet of problem
$params = [
'index' => $newIndexName,
];
$this->esClient->indices()->create($params));
System details
- Operating System Ubuntu 18.04
- PHP Version 7.4.4
- ES-PHP client version 7.6.1
- Elasticsearch version 7.4
@GinoPane you mentioned that the issue is a trailing slash
in your setting. Can you write an example to reproduce the issue? Thanks!
Hi @ezimuel! So here's the example how I build the client:
class ClientFactory
{
/**
* @param string $host
* @param string $region
* @return Client
*/
public static function create(string $host, string $region): Client
{
return ClientBuilder::create()
->setHandler(new ElasticsearchPhpHandler($region))
->setHosts([$host])
->build();
}
}
And initially the host looked like this: https://vpc-elastic-green-[...].eu-west-2.es.amazonaws.com/
When I made a small change in the config and the host became like this: https://vpc-elastic-green-[...].eu-west-2.es.amazonaws.com, the issue disappeared. Now I have rtrim('/')
in my factory just in case :smile:
Thanks @GinoPane. I confirm that the issue comes when you add a trailing slash
in your host URL. Interesting the issue happens only for some endpoints, like indices.create
.
Do you want to send a PR to fix it? You need also to provide a unit test for that. Let me know, otherwise I'll do it. Thanks!
@ezimuel please do. I'm afraid I can provide only a workaround (like required rtrim()
for host) but it is a fix for a symptom.
Don't worry, I'll provide the fix. Thanks for the report!
I have the same issue. I am using elastic.co service and it was working perfectly until yesterday. Suddenly it started to throw an error.
Elasticsearch\Common\Exceptions\BadRequest400Exception : {"error":"Incorrect HTTP method for uri [//auto_complete_local_1] and method [PUT], allowed: [POST]","status":405}
Everything looks good in logs. No idea what is the reason.
Same here. Debugging I see that there is indeed a GET /myindex/_source/
returning a 405 Method Not Allowed
and the library is translating that message to Incorrect HTTP method for uri [/myindex/_source/] and method [GET], allowed: [POST]
.
I solved this in the end by removing slash from my requests but this is super weird how it is started to giving errors out of nowhere. Contacted support about this but they said nothing changed on their side.