{"id":5362,"date":"2018-09-27T02:32:23","date_gmt":"2018-09-27T02:32:23","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=5362"},"modified":"2025-05-04T06:08:23","modified_gmt":"2025-05-04T06:08:23","slug":"understanding-rest-http-method-get-post-put-head-delete","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/understanding-rest-http-method-get-post-put-head-delete\/","title":{"rendered":"Understanding REST HTTP method &#8211; GET, POST, PUT, HEAD, DELETE in Elasticsearch"},"content":{"rendered":"<p>REST (Representational State Transfer) was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation. REST is an architectural style for designing distributed systems. It is not a standard but a set of constraints, such as being stateless, having a client\/server relationship, and a uniform interface. REST is not strictly related to HTTP, but it is most commonly associated with it<\/p>\n<p><strong>Principles of REST<\/strong><\/p>\n<ul>\n<li>Resources expose easily understood directory structure URIs.<\/li>\n<li>Representations transfer JSON or XML to represent data objects and attributes.<\/li>\n<li>Messages use HTTP methods explicitly (for example, GET, POST, PUT, and DELETE).<\/li>\n<li>Stateless interactions store no client context on the server between requests. State dependencies limit and restrict<\/li>\n<li>scalability. The client holds session state.<\/li>\n<\/ul>\n<p><strong>HTTP methods<\/strong><br \/>Use HTTP methods to map CRUD (create, retrieve, update, delete) operations to HTTP requests.<\/p>\n<p><strong>GET (Retrieve information)<\/strong><br \/>Retrieve information. GET requests must be safe and idempotent, meaning regardless of how many times it repeats with the same parameters, the results are the same. They can have side effects, but the user doesn&#8217;t expect them, so they cannot be critical to the operation of the system. Requests can also be partial or conditional. example<\/p>\n<p><strong>POST (Create + Update)<\/strong><br \/>Request that the resource at the URI do something with the provided entity. Often POST is used to create a new entity, but it can also be used to update an entity.<\/p>\n<p><strong>PUT (Create + Update)<\/strong><br \/>Store an entity at a URI. PUT can create a new entity or update an existing one. A PUT request is idempotent. Idempotency is the main difference between the expectations of PUT versus a POST request.<\/p>\n<p><strong>DELETE (Remove)<\/strong><br \/>Request that a resource be removed; however, the resource does not have to be removed immediately. It could be an asynchronous or long-running request.<\/p>\n<p><a href=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/http-method-put-post.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-12802\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/http-method-put-post.jpg\" alt=\"\" width=\"653\" height=\"354\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/http-method-put-post.jpg 653w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/http-method-put-post-300x163.jpg 300w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/a><\/p>\n<p>\u00a0<\/p>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"753\" height=\"491\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/curl-http-rest-api-request-response.jpg\" alt=\"\" class=\"wp-image-12799\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/curl-http-rest-api-request-response.jpg 753w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/curl-http-rest-api-request-response-300x196.jpg 300w\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" \/><\/figure>\n\n\n\n<p><strong>Status codes indicate the result of the HTTP request.<\/strong><\/p>\n\n\n\n<p>1XX &#8211; informational<br>2XX &#8211; success<br>3XX &#8211; redirection<br>4XX &#8211; client error<br>5XX &#8211; server error<\/p>\n\n\n\n<p><strong>PUT vs. POST in RESTful in Elastic Search<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"799\" height=\"223\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/put-vs-post-.jpg\" alt=\"\" class=\"wp-image-12800\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/put-vs-post-.jpg 799w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/put-vs-post--300x84.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/put-vs-post--768x214.jpg 768w\" sizes=\"auto, (max-width: 799px) 100vw, 799px\" \/><\/figure>\n\n\n\n<p>A PUT request is idempotent. Idempotency is the main difference between the expectations of PUT versus a POST request.<\/p>\n\n\n\n<p><strong>PUT implies putting a resource<\/strong> &#8211; <strong>completely replacing<\/strong> whatever is available at the given URL with a different thing. By definition, a<strong> PUT is idempotent<\/strong>. Do it as many times as you like, and the result is the same. x=5 is idempotent. <\/p>\n\n\n\n<p>You can PUT a resource whether it previously exists, or not (eg, to<strong> Create, or to Update<\/strong>)!<\/p>\n\n\n\n<p><strong>POST updates a resource<\/strong> and <strong>adds a subsidiary resource<\/strong>, <strong>or<\/strong> <strong>causes a change<\/strong>. A POST is <strong>not idempotent<\/strong>, in the way that x++ is not idempotent.<\/p>\n\n\n\n<p><strong>Good read here<\/strong><br>https:\/\/stackoverflow.com\/questions\/630453\/put-vs-post-in-rest<\/p>\n\n\n\n<p>Overall, The PUT is used for update the existing values and POST is using to insert a new value.<\/p>\n\n\n\n<p><strong>At this URL(PUT) vs under this URL(POST)<\/strong><\/p>\n\n\n\n<p>PUT verb (&#8220;store this document <strong>at this URL<\/strong>&#8220;), we use the POST verb (&#8220;store this document <strong>under this URL<\/strong>&#8220;).<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">PUT \/website\/blog\/<span class=\"hljs-number\">123<\/span>\n{\n  <span class=\"hljs-string\">\"title\"<\/span>: <span class=\"hljs-string\">\"My first blog entry\"<\/span>,\n  <span class=\"hljs-string\">\"text\"<\/span>:  <span class=\"hljs-string\">\"Just trying this out...\"<\/span>,\n  <span class=\"hljs-string\">\"date\"<\/span>:  <span class=\"hljs-string\">\"2014\/01\/01\"<\/span>\n}\n\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"284\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/put-vs-post-patch.jpg\" alt=\"\" class=\"wp-image-12803\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/put-vs-post-patch.jpg 771w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/put-vs-post-patch-300x111.jpg 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/04\/put-vs-post-patch-768x283.jpg 768w\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/figure>\n\n\n\n<p><strong>Auto-generation of ids(POST) vs specify an id(PUT)<\/strong><\/p>\n\n\n\n<p><strong>Autogenerated IDs<\/strong> are 20 character long, URL-safe, Base64-encoded GUID strings. These GUIDs are generated from a modified FlakeID scheme which allows multiple nodes to be generating unique IDs in parallel with essentially zero chance of collision.<\/p>\n\n\n\n<p>Thus, only difference, that a POST can be used to achieve <strong>auto-generation of ids<\/strong> whereas a PUT is used when you want to <strong>specify an id<\/strong>.<\/p>\n\n\n\n<p>It&#8217;s all about REST semantics. <strong>POST <\/strong>basically that you are posting a request which is going to modify the server state. POST index\/type { &#8220;foo&#8221;: &#8220;bar&#8221; } will generate an _id server side and will index the document with this _id. <\/p>\n\n\n\n<p><strong>PUT <\/strong>is used to send a resource to the server. PUT index\/type\/id { &#8220;foo&#8221;: &#8220;bar&#8221; } will put or update a document named index\/type\/id in the server. <\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/0e3c6f139020127e280b14282aba6b2a.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>REST (Representational State Transfer) was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation. REST is an architectural style for designing distributed systems. It is not a standard but a set of constraints, such as being stateless, having a client\/server relationship, and a uniform interface. REST is not strictly related to HTTP,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5936],"tags":[352,4666,1663,5061,5059,5060],"class_list":["post-5362","post","type-post","status-publish","format-standard","hentry","category-elastic","tag-delete","tag-elasticsearch","tag-get","tag-head","tag-post","tag-put"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5362"}],"version-history":[{"count":10,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5362\/revisions"}],"predecessor-version":[{"id":12804,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5362\/revisions\/12804"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}