Difference between XPOST and XPUT

14,009

Solution 1

Generally when using a REST API:
- POST is used to create a resource, where the server will pick an ID.
- PUT is used to update OR PLACE a resource at a known ID.

Doc creation examples in the ES documentation show the caller picking an ID.

Like so:

curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}'

Since the caller is picking the ID a PUT seems appropriate here.

BUT

using POST Elasticsearch can also generate an ID for you.

$ curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}'

Solution 2

Somehow they have the same functionality with small different

PUT verb (“store this document at this URL”)

POST verb (“store this document under this URL”)

in the put you must indicate the exact URL but in the post you can set document by just type

for example:

PUT /website/blog/123 says put this document at exact this URL but POST /website/blog will insert the document in blog and auto increment the id of the last document.

Share:
14,009
a1mery
Author by

a1mery

SharePoint Developer for 5 years.

Updated on August 16, 2022

Comments

  • a1mery
    a1mery over 1 year

    I'm just starting to use ElasticSearch. And I tried to know how to insert documents. I only found examples using the PUT method : $ curl -XPUT 'http://localhost:9200/...' But it also seems to work using POST. Is there any difference between these two methods?

    Thank you.