When to use PUT versus POST?


(Amit Saxena) #1

Hi all,

I have just started learning Elasticsearch. While going through the online documentation which talks about use of REST APIs, I have observed that whenever a create / modify / delete action is required, sometimes POST is used as a verb and rest of the times PUT is used in REST API syntax.

Is there a logical criteria to decide when to use PUT versus POST in REST APIs ?

Regards,
Amit Saxena


(David Pilato) #2

It's all about REST semantics.

POST basically that you are posting a request which is going to modify the server state.

POST index/type
{
  "foo": "bar"
}

will generate an _id server side and will index the document with this _id.

PUT is used to send a resource to the server.

PUT index/type/id
{
  "foo": "bar"
}

will put or update a document named index/type/id in the server.

Makes sense?

More details here: https://en.wikipedia.org/wiki/Representational_state_transfer


What is the difference between POST an PUT in the ElasticSearch API?
Why POST for partial update?
(Nik Everett) #3

And our understanding of the semantics at the time when we made the APIs. And backwards compatibility constraints. And whatever "feels" natural to the person who implemented the API.

Where it makes a lot of sense Elasticsearch maps the HTTP verbs to useful things. But when it doesn't make a ton of sense we just go with whatever verb feels good rather than trying to be super strict about REST. Also, we don't do linked data, instead relying on you to build links from context. I'm told that is particularly non-REST. But it is what we do.


(Amit Saxena) #4

Hi David,

Thanks for the reply.

Your's explanation definitely helped in clearing the confusion to a lot extent.

Specifically the example mentioned by you explains this. I also had a look at the documentation and found the same described in https://www.elastic.co/guide/en/elasticsearch/reference/current/_modifying_your_data.html .

While the majority of the confusion is clear, however from the documentation, I found at least one instance where "POST" was used even in case where explicit "_id" field was specified. The example was taken from "Batch Processing" instance. The documentation, along with the example, was taken from https://www.elastic.co/guide/en/elasticsearch/reference/current/_batch_processing.html .

Please help and suggest if I should taken the above case as a specific exception and generally go with the understanding you have mentioned earlier.

Regards,
Amit Saxena


(Amit Saxena) #5

Hi Nik,

I am still OK with this however just wanted a method / approach to remember where to use "PUT" versus "POST". I haven't tried using "PUT" versus "POST" in command line as I don't have an instance available to practice on however I will surely try it to observe the error message.

Regards,
Amit Saxena


(system) #6

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