Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between XPOST and XPUT

I am learning Elasticsearch, I found that XPOST and XPUT are in general the same when 'update' or 'replace' documents. They all change the field values.

curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
  "name": "Jane Doe"
}'

curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
  "doc": { "name": "Jane Doe" }
}'

So they all changed the name field to "Jane Doe". I am wondering whats the difference between XPOST and XPUT in the above context.

like image 818
daiyue Avatar asked Nov 30 '22 16:11

daiyue


2 Answers

The two commands are not at all the same. The first one (with PUT) will update a full document, not only the field you're sending.

The second one (with POST) will do a partial update and only update the fields you're sending, and not touch the other ones already present in the document.

like image 103
Val Avatar answered Dec 06 '22 06:12

Val


firstly, -X is a flag of curl. please see -X in the man page. It is same as --request. You can specify which HTTP method to use (POST, GET, PUT, DELETE etc) http://curl.haxx.se/docs/manpage.html

Regarding POST and PUT, they are HTTP methods or "verbs".

ElasticSearch provides us with a REST API. According to REST practices, POST is for create and PUT is for updating a record.

Please see: http://www.restapitutorial.com/lessons/httpmethods.html

like image 37
Jya Avatar answered Dec 06 '22 04:12

Jya