pythonelasticsearchpython-requestshttp-status-code-405http-status-code-406

POST a document to an Elasticsearch index using requests?


I am having difficulties with this: all the following return 405, and I have no idea why:

    data = {
        "xxx" : "status",
        "value" : "incomplete"
    }
    headers = {'Content-type': 'application/json'}
    response = requests.post(f'http://localhost:9200/my_index', data=json.dumps(data)) 
    response = requests.post(f'http://localhost:9200/my_index', data=json.dumps(data), headers=headers) 
    response = requests.post(f'http://localhost:9200/my_index', json=data)
    response = requests.post(f'http://localhost:9200/my_index', json=data, headers=headers) 

NB I have also tried with "http://localhost:9200/my_index/_doc" instead: they all get 406.

ES is definitely running, I am able to use Postman, and curl at the CLI.

requests.put with the above attempts (ending ".../_doc/1") gets either 406 or 401, except for one of the above:

response = requests.put(f'http://localhost:9200/my_index/_doc/2', data=json.dumps(data), headers=headers)

... which works (201 or 200 depending on whether the index already exists).

OS is Linux Mint 20. ES is 7.16.3.


Solution

  • According to the docs, you must put _doc after the index name:

    PUT /<target>/_doc/<_id>
    
    POST /<target>/_doc/
    

    You can choose whether Elastic will create the id for you, or you can also specify an id.

    Index API