# About the \`index()\` method of \`Elasticsearch Python Client\` library, is it using PUT or POST?

**URL:** <https://discuss.elastic.co/t/about-the-index-method-of-elasticsearch-python-client-library-is-it-using-put-or-post/335920>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [June 13, 2023, 10:41pm UTC](https://discuss.elastic.co/t/about-the-index-method-of-elasticsearch-python-client-library-is-it-using-put-or-post/335920 "2023-06-13T22:41:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Z](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike_z/32/122216_2.png) [@Mike\_Z](https://discuss.elastic.co/u/Mike_Z)\
**Post date:** [June 13, 2023, 10:41pm UTC](https://discuss.elastic.co/t/about-the-index-method-of-elasticsearch-python-client-library-is-it-using-put-or-post/335920/1 "2023-06-13T22:41:27Z")

</div>

We are looking into a Python library [`Elasticsearch Python Client`](https://github.com/elastic/elasticsearch-py), and its official online document contains the below example for ingesting data into Elastic.

We hope to make the ingest action idempotent, so we wonder whether the library's `index()` method uses `PUT` or `POST`.

Please also point out if we should investigate something else for the idempotent feature. We are new to Elasticsearch and its Python libraries, so we highly appreciate any hints and suggestions.

**Details:**

[This reference](https://www.w3schools.com/tags/ref_httpmethods.asp) says:

> **The PUT Method**  
> The difference between POST and PUT is that PUT requests are idempotent. ...

**The example in the official online document:**

```python
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch('https://localhost:9200')

doc = {
    'author': 'author_name',
    'text': 'Interensting content...',
    'timestamp': datetime.now(),
}
resp = es.index(index="test-index", id=1, document=doc)
print(resp['result'])

```

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [June 13, 2023, 11:35pm UTC](https://discuss.elastic.co/t/about-the-index-method-of-elasticsearch-python-client-library-is-it-using-put-or-post/335920/2 "2023-06-13T23:35:24Z")

</div>

See [Index API | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#docs-index-api-request);

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

You can see that a `PUT` is when you want to specify an `_id`, and a `POST` is when you want to auto create one, which follows the W3 reference you linked to.

That means your quoted example will be a `PUT` when it sends the request.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 11, 2023, 11:35pm UTC](https://discuss.elastic.co/t/about-the-index-method-of-elasticsearch-python-client-library-is-it-using-put-or-post/335920/3 "2023-07-11T23:35:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
