{"id":3165,"date":"2019-03-06T10:16:25","date_gmt":"2019-03-06T10:16:25","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3165"},"modified":"2019-03-16T17:37:22","modified_gmt":"2019-03-16T17:37:22","slug":"get-vs-put-vs-post-vs","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/get-vs-put-vs-post-vs\/","title":{"rendered":"Get vs Put vs Post vs Delete"},"content":{"rendered":"<p><strong>GET<\/strong><\/p>\n<ol>\n<li>GET is idempotent and can be requested any number of times<\/li>\n<li>GET requests can be cached, can be distributed &#038; shared<\/li>\n<li>GET request is less secured compared to POST.<\/li>\n<\/ol>\n<p><strong>POST<\/strong><\/p>\n<ol>\n<li>Used to <em>Create <\/em>a resource<\/li>\n<li>Post is not idempotent.x++ is not idempotent<\/li>\n<li>POST is NOT idempotent. So if you retry the request N times, you will end up having N resources with N different URIs created on server.<\/li>\n<\/ol>\n<p><strong>PUT<\/strong> <\/p>\n<ol>\n<li>Used to <em>Create <\/em>or <em>Modify <\/em>a resource<\/li>\n<li>PUT is idempotent, so if you PUT an object twice, it has no effect.<\/li>\n<li>x=5 is idempotent.You can PUT a resource whether it previously exists, or not (eg, to Create, or to Update)!<\/li>\n<\/ol>\n<p><strong>When to use Put and Post<\/strong><br \/>\nYou can use both PUT or POST for creating the resource until the client decides the resource location in the Server.But if the server decides the resource location using <\/p>\n<pre>\r\nPOST \/questions\/<existing_question> HTTP\/1.1\r\nHost: www.example.com\/\r\n<\/pre>\n<p>Note that the following is an error:<\/p>\n<pre>\r\nPOST \/questions\/<new_question> HTTP\/1.1\r\nHost: www.example.com\/\r\n<\/pre>\n<p>If the URL is not yet created, you should not be using POST to create it while specifying the name. This should result in a &#8216;resource not found&#8217; error because <new_question> does not exist yet. You should PUT the <new_question> resource on the server first.<\/p>\n<p>You could though do something like this to create a resources using POST:<\/p>\n<pre>\r\nPOST \/questions HTTP\/1.1\r\nHost: www.example.com\/\r\n<\/pre>\n<p>Note that in this case the resource name is not specified, the new objects URL path would be returned to you.<\/p>\n<p>PUT is Used to create a resource, or overwrite it. While you specify the resources new URL.<\/p>\n<pre>\r\nPUT \/questions\/<new_question> HTTP\/1.1\r\nHost: www.example.com\/\r\n<\/pre>\n<p>To overwrite an existing resource:<\/p>\n<pre>\r\nPUT \/questions\/<existing_question> HTTP\/1.1\r\nHost: www.example.com\/\r\n<\/pre>\n<p><strong>PATCH<\/strong><br \/>\nPatch request says that we would only send the data that we need to modify without modifying or effecting other parts of the data. Ex: if we need to update only the first name, we pass only the first name.PATCH &#8211; HTTP.PATCH can be used when the client is sending one or more changes to be applied by the server. The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI. The set of changes is represented in a format called a patch document.<\/p>\n<p>In PUT request,<strong> the enclosed entity would be considered as the modified version of a resource<\/strong> which residing on server and it would be replaced by this modified entity.<\/p>\n<p>In PATCH request,<strong> enclosed entity contains the set of instructions that how the entity which residing on server, would be modified<\/strong> to produce a newer version.<\/p>\n<p><strong>DELETE<\/strong><\/p>\n<p>DELETE is pretty easy to understand. It is used to delete a resource identified by a URI.On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response (see Return Values below). Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GET GET is idempotent and can be requested any number of times GET requests can be cached, can be distributed &#038; shared GET request is less secured compared to POST. POST Used to Create a resource Post is not idempotent.x++ is not idempotent POST is NOT idempotent. So if you retry the request N times,&hellip; <a href=\"https:\/\/codethataint.com\/blog\/get-vs-put-vs-post-vs\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266],"tags":[],"class_list":["post-3165","post","type-post","status-publish","format-standard","hentry","category-concepts-rest"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3165"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3165\/revisions"}],"predecessor-version":[{"id":3226,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3165\/revisions\/3226"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}