elasticsearch-php icon indicating copy to clipboard operation
elasticsearch-php copied to clipboard

Incorrect HTTP method for uri [/] and method [PUT] when trying to create an index

Open GinoPane opened this issue 4 years ago • 8 comments

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 avatar Apr 08 '20 13:04 GinoPane

@GinoPane you mentioned that the issue is a trailing slash in your setting. Can you write an example to reproduce the issue? Thanks!

ezimuel avatar Apr 09 '20 09:04 ezimuel

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:

GinoPane avatar Apr 09 '20 10:04 GinoPane

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 avatar Apr 10 '20 15:04 ezimuel

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

GinoPane avatar Apr 10 '20 15:04 GinoPane

Don't worry, I'll provide the fix. Thanks for the report!

ezimuel avatar Apr 10 '20 15:04 ezimuel

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

faridmovsumov avatar Jun 11 '20 20:06 faridmovsumov

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

cawoodm avatar Jan 25 '21 12:01 cawoodm

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.

faridmovsumov avatar Jan 25 '21 15:01 faridmovsumov