Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between XPOST and XPUT

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.

like image 210
a1mery Avatar asked May 31 '14 18:05

a1mery


People also ask

What is the difference between put X and put X?

PUT x (if x identifies a resource ): "Replace the content of the resource identified by x with my content." PUT x (if x does not identify a resource): "Create a new resource containing my content and use x to identify it."

What is the difference between post and put methods?

It essentially means that POST request-URI should be of a collection URI. PUT method is idempotent. So if we retry a request multiple times, that should be equivalent to a single request invocation.

What is the difference between post and put Uris?

UUID's are superior in every way except storage space. Ints for IDs are a holdover from when DB storage was a larger concern that it is today. The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity.

What is the difference between put and post in Salesforce?

PUT works as specific while POST work as abstract. If you send the same PUT request multiple times, the result will remain the same but if you send the same POST request multiple times, you will receive different results. PUT method is idempotent whereas POST method is not idempotent.


1 Answers

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"
}'
like image 171
mconlin Avatar answered Oct 16 '22 08:10

mconlin