{"id":6483,"date":"2012-03-20T18:50:39","date_gmt":"2012-03-20T10:50:39","guid":{"rendered":"http:\/\/ihower.tw\/blog\/?p=6483"},"modified":"2025-07-04T07:26:34","modified_gmt":"2025-07-03T23:26:34","slug":"http-verbs-post-put-patch","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/6483-http-verbs-post-put-patch","title":{"rendered":"HTTP Verbs: \u8ac7 POST, PUT \u548c PATCH \u7684\u61c9\u7528"},"content":{"rendered":"<blockquote><p>\u95dc\u65bc HTTP \u57fa\u672c\u77e5\u8b58\uff0c\u53ef\u4ee5\u53c3\u8003\u6211\u7684 <a href=\"https:\/\/ihower.tw\/cs\/networking-http.html\">\u7db2\u8def\u6982\u8ad63: HTTP<\/a><\/p><\/blockquote>\n<p>\u5728\u521d\u5b78REST\u7684\u9019\u5e7e\u5e74\uff0c\u6211\u90fd\u8a8d\u70ba\u9019\u5e7e\u500b HTTP Verbs \u5c31\u662f\u5c0d\u61c9 CRUD\uff1a<\/p>\n<ul>\n<li>POST = \u65b0\u589e<\/li>\n<li>GET = \u8b80\u53d6<\/li>\n<li>PUT = \u66f4\u65b0<\/li>\n<li>DELETE = \u522a\u9664<\/li>\n<\/ul>\n<p>\u5f8c\u4f86\u5728\u8a2d\u8a08 API only \u7684 Web service \u6642\uff0c\u5e38\u5e38\u641e\u4e0d\u6e05\u695a\u5230\u5e95\u8981\u7528 PUT \u9084\u662f POST\uff0c\u624d\u767c\u73fe\u6211\u88ab Rails \u7684\u9df9\u67b6\u7bc4\u4f8b\u8aa4\u5c0e\u4e86(\u88ab\u6846\u67b6\u6846\u4f4f\u60f3\u6cd5\u4e86?)\uff0c\u6240\u8b02\u7684 PUT \u5176\u5be6\u4e5f\u53ef\u4ee5\u7528\u5230\u65b0\u589e\uff0c\u800c\u4e14\u9084\u6709\u4e00\u500b\u883b\u65b0\u7684 HTTP Verb \u53eb\u505a PATCH\uff0c\u50cf <a href=\"http:\/\/developer.github.com\/v3\/#http-verbs\">Github API<\/a> \u548c <a href=\"http:\/\/weblog.rubyonrails.org\/2012\/2\/25\/edge-rails-patch-is-the-new-primary-http-method-for-updates\/\">Rails 4<\/a> \u90fd\u958b\u59cb\u63a1\u7528\u3002<\/p>\n<p>PUT \u6bd4\u8f03\u6b63\u78ba\u7684\u5b9a\u7fa9\u662f Replace (Create or Update)\uff0c\u4f8b\u5982<code>PUT \/items\/1<\/code>\u7684\u610f\u601d\u662f\u66ff\u63db<code>\/items\/1<\/code>\uff0c\u5982\u679c\u5df2\u7d93\u5b58\u5728\u5c31\u66ff\u63db\uff0c\u6c92\u6709\u5c31\u65b0\u589e\u3002PUT \u5fc5\u9808\u5305\u542b<code>items\/1<\/code>\u7684\u6240\u6709\u5c6c\u6027\u8cc7\u6599\u3002<\/p>\n<p>\u4f46\u662f\u9019\u500b\u884c\u70ba\u4f3c\u4e4e\u4e0d\u600e\u9ebc\u597d\u7528\uff0c\u5982\u679c\u53ea\u662f\u70ba\u4e86\u66f4\u65b0<code>items\/1<\/code>\u7684\u5176\u4e2d\u4e00\u500b\u5c6c\u6027\uff0c\u5c31\u9700\u8981\u91cd\u50b3\u6240\u6709<code>items\/1<\/code>\u7684\u5c6c\u6027\u4e5f\u592a\u6d6a\u8cbb\u983b\u5bec\u4e86\uff0c\u6240\u4ee5\u5f8c\u4f86\u53c8\u6709\u65b0\u7684 <a href=\"http:\/\/tools.ietf.org\/html\/rfc5789\">PATCH Method<\/a> \u6a19\u6e96\uff0c\u53ef\u4ee5\u7528\u4f86\u505a\u90e8\u5206\u66f4\u65b0(Partial Update)\u3002<\/p>\n<p>\u7528\u5e7e\u500b Ruby code \u4f86\u8209\u4f8b\u5427\uff1a<\/p>\n<p>POST \u65b0\u589e\uff1a<\/p>\n<pre><code># POST \/items\ndef create\n  @item = Item.new\n  @item.attributes = { :name =&gt; params[:name],\n                      :image =&gt; params[:image] }\n  @item.save\nend\n<\/code><\/pre>\n<p>PUT \u66ff\u63db(\u65b0\u589e\u6216\u5b8c\u6574\u66f4\u65b0)\uff0c\u6b64\u4f8b\u4e2d\u5982\u679cimage\u53c3\u6578\u6c92\u6709\u50b3\uff0c\u6703\u88ab\u66f4\u65b0\u6210\u7a7a\uff1a<\/p>\n<pre><code># PUT \/items\/{id}\ndef replace\n   @item = Item.find_by_id(params[:id])\n\n  unless @item  # if @item.nil?\n      @item = Item.new\n      @item.id = params[:id]\n  end\n\n  @item.attributes = { :name =&gt; params[:name],\n                       :image =&gt; params[:image] }\n  @item.save\nend\n<\/code><\/pre>\n<p>PATCH \u90e8\u5206\u66f4\u65b0\uff0c\u6b64\u4f8b\u4e2d\u5982\u679cimage\u53c3\u6578\u6c92\u6709\u50b3\uff0c\u5c31\u4e0d\u6703\u88ab\u66f4\u65b0\uff1a<\/p>\n<pre><code># PATCH \/items\/{id}\ndef patch\n   @item = Item.find(params[:id])\n  @item.attributes = params.slice(:name, :image)\n  @item.save\nend\n<\/code><\/pre>\n<p>DELETE \u522a\u9664\uff0c\u6b64\u4f8b\u4e2d\u7121\u8ad6\u5982\u4f55 items\/1 \u6700\u5f8c\u90fd\u4e0d\u5b58\u5728\u4e86<\/p>\n<pre><code># DELETE \/items\/{id}\ndef destroy\n   @item = Item.find_by_id(params[:id])\n  @item.destroy if @item\nend\n<\/code><\/pre>\n<p>\u6709\u6642\u5019\u62d8\u6ce5\u65bc&#8221;\u8a9e\u610f&#8221;\u9019\u4ef6\u4e8b\u60c5\u4e0d\u5bb9\u6613\u60f3\u6e05\u695a\u8a2d\u8a08 REST API \u6642\u8981\u7528\u54ea\u4e00\u500b HTTP \u65b9\u6cd5\uff0c\u56e0\u70ba\u6709\u6642\u5019\u4e0d\u4e00\u5b9a\u662fCRUD\u7684\u5f62\u5f0f\u3002\u6211\u8a8d\u70ba <a href=\"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec9.html\">9.1 Safe and Idempotent Methods<\/a> \u5b9a\u7fa9\u4e2d\u7684 \u201cIdempotent\u201d \u7279\u6027\u883b\u5be6\u7528\u7684\u3002idempotent \u7684\u610f\u601d\u662f\u5982\u679c\u76f8\u540c\u7684\u64cd\u4f5c\u518d\u57f7\u884c\u7b2c\u4e8c\u904d\u7b2c\u4e09\u904d\uff0c\u7d50\u679c\u9084\u662f\u8ddf\u7b2c\u4e00\u904d\u7684\u7d50\u679c\u4e00\u6a23 (\u4e5f\u5c31\u662f\u8aaa\u4e0d\u7ba1\u57f7\u884c\u5e7e\u6b21\uff0c\u7d50\u679c\u90fd\u8ddf\u53ea\u6709\u57f7\u884c\u4e00\u6b21\u4e00\u6a23)\u3002\u6839\u64da HTTP \u7684\u898f\u683c\uff0cGET, HEAD, PUT \u548c DELETE \u662f idempotent\uff0c\u76f8\u540c\u7684 Request \u518d\u57f7\u884c\u4e00\u6b21\uff0c\u7d50\u679c\u9084\u662f\u4e00\u6a23\u3002\u53ea\u6709 POST \u548c PATCH \u4e0d\u662f idempotent\uff0cPOST \u518d\u57f7\u884c\u4e00\u904d\uff0c\u6703\u518d\u65b0\u589e\u4e00\u7b46\u8cc7\u6599\uff0cPATCH \u5247\u662f\u6709\u4e0d\u80fd\u4fdd\u8b49 idempotent \u7684\u53ef\u80fd\u6027(\u5fb5\u6c42\u4f8b\u5b50)\u3002POST \u548c PATCH \u90fd\u4e0d\u662f idempotent \u7684\u64cd\u4f5c\uff0c\u9019\u4e5f\u662f\u70ba\u4ec0\u9ebc <a href=\"http:\/\/developer.github.com\/v3\/#http-verbs\">Github API<\/a> \u88e1\u7528 POST \u7576\u505a PATCH \u7684\u76f8\u5bb9\u53d6\u4ee3\u65b9\u6848\u3002<\/p>\n<p>\u53e6\u4e00\u500b HTTP Methods \u7279\u6027\u662f&#8221;Safe&#8221;\uff0c\u9019\u6bd4\u8f03\u7c21\u55ae\uff0c\u53ea\u6709 GET \u548c HEAD \u662f Safe \u64cd\u4f5c\u3002Safe \u7279\u6027\u6703\u5f71\u97ff\u662f\u5426\u53ef\u4ee5\u5feb\u53d6(POST\/PUT\/PATCH\/DELETE \u4e00\u5b9a\u90fd\u4e0d\u53ef\u4ee5\u5feb\u53d6)\u3002\u800c Idempotent \u7279\u6027\u5247\u662f\u6703\u5f71\u97ff\u53ef\u5426 Retry (\u91cd\u8a66\uff0c\u53cd\u6b63\u7d50\u679c\u4e00\u6a23)\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<th><\/th>\n<th>Safe?<\/th>\n<th>Idempotent?<\/th>\n<\/tr>\n<tr>\n<th>GET<\/th>\n<td>Y<\/td>\n<td>Y<\/td>\n<\/tr>\n<tr>\n<th>POST<\/th>\n<td>N<\/td>\n<td>N<\/td>\n<\/tr>\n<tr>\n<th>PATCH<\/th>\n<td>N<\/td>\n<td>N<\/td>\n<\/tr>\n<tr>\n<th>PUT<\/th>\n<td>N<\/td>\n<td>Y<\/td>\n<\/tr>\n<tr>\n<th>DELETE<\/th>\n<td>N<\/td>\n<td>Y<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u900f\u904e Idempotent \u7684\u7279\u6027\uff0c\u6709\u6642\u5019\u53ef\u4ee5\u5e6b\u52a9\u4f60\u5224\u65b7\u8a72\u7528\u54ea\u4e00\u500b HTTP Methods\u3002\u56de\u5230\u524d\u9762\u8b1b PUT \u597d\u50cf\u4e0d\u592a\u597d\u7528\uff0c\u4f8b\u5982\u4ee5\u700f\u89bd\u5668\u70ba\u4e3b\u7684 HTML \u61c9\u7528\u8868\u55ae\uff0c\u8981\u9ebb\u662f POST \u65b0\u589e\u8cc7\u6599\uff0c\u8981\u9ebb\u5c31\u662f\u7528 PATCH \u90e8\u5206\u66f4\u65b0\u5df2\u7d93\u5b58\u5728\u7684\u8cc7\u6599(\u4f60\u4e0d\u6703\u5e0c\u671b\u7528 PUT \u4fee\u6539\u500b\u4eba\u8cc7\u6599\u7684\u6642\u5019\uff0c\u6bcf\u6b21\u90fd\u8981\u91cd\u50b3\u7167\u7247\u5427)\uff0c\u56e0\u6b64\u6bd4\u8f03\u5c11\u7528\u5230 PUT\u3002\u9019\u4e5f\u662f\u70ba\u4ec0\u9ebc Rails 4 \u628a\u8868\u55ae\u4fee\u6539\u7684 PUT \u6539\u6210 PATCH Method\uff0c\u900f\u904e Rails \u9df9\u67b6\u7522\u751f\u51fa\u4f86\u7684 update\uff0c\u5176\u5be6\u7b26\u5408\u7684\u662f PATCH \u884c\u70ba\u800c\u4e0d\u662f PUT\u3002<\/p>\n<p>\u4e0d\u904e\u9084\u662f\u6709\u4e00\u4e9b\u6211\u8a8d\u70ba\u883b\u9069\u5408\u7528PUT\u7684\u60c5\u5883\uff0c\u4f8b\u5982\u8a02\u95b1\u6771\u897f\u8a72\u7528POST\u9084\u662fPUT\u5462?<\/p>\n<pre><code>POST \/subscriptions\n# \u9084\u662f\nPUT \/subscriptions\/{something_id}\n<\/code><\/pre>\n<p>\u8a02\u95b1\u6771\u897f\u53ea\u6709\u5169\u500b\u72c0\u614b\uff0c&#8221;\u5df2\u8a02\u95b1&#8221;\u6216&#8221;\u6c92\u6709\u8a02\u95b1&#8221;\uff0c\u9019\u500b\u8a02\u95b1\u7684\u64cd\u4f5c\u518d\u91cd\u9001\u5e7e\u6b21\uff0c\u9084\u662f&#8221;\u5df2\u8a02\u95b1&#8221;\uff0c\u6240\u4ee5\u6211\u8a8d\u70ba\u883b\u7b26\u5408 PUT \u7684 idempotent \u7279\u6027\u3002\u800c\u5c0d\u61c9\u7684\u53d6\u6d88\u8a02\u95b1 API \u60f3\u7576\u7136\u5c31\u662f<\/p>\n<pre><code>DELETE \/subscriptions\/{something_id}\n<\/code><\/pre>\n<p>\u53e6\u5916\u4e00\u500b\u6211\u89ba\u5f97\u6709\u8da3\u53c8\u5be6\u7528\u7684 PUT \u4f8b\u5b50\u662f\uff0c\u8a2d\u8a08 API \u7ed9\u53ef\u4ee5\u96e2\u7dda offline \u4f7f\u7528\u7684\u884c\u52d5\u88dd\u7f6e(\u4f8b\u5982iPhone)\u3002\u652f\u63f4 offline \u7522\u751f\u7684\u8cc7\u6599\uff0c\u901a\u5e38\u6703\u4f7f\u7528 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Universally_unique_identifier\">UUID<\/a> \u4f86\u7522\u751f ID\uff0c\u9019\u6a23\u5c31\u4e0d\u9700\u8981\u900f\u904e\u4e2d\u592e\u4f3a\u670d\u5668\u7ba1\u63a7 ID\uff0c\u65b9\u4fbf\u88dd\u7f6e\u4e4b\u9593\u7684\u540c\u6b65\u3002\u9019\u6a23\u7684\u60c5\u5883\u4e0b\uff0c\u65b0\u589e\u8cc7\u6599\u7684 REST API \u5176\u5be6\u53ef\u4ee5\u63d0\u4f9b\u5169\u7a2e\uff1a<\/p>\n<pre><code>POST \/items # \u53c3\u6578\u5e36 uuid=4937973d-e349-460a-a6ad-38625125b24a\u3002\u5982\u679c\u4e0d\u5e36uuid\u5247\u7531server\u4f86\u7522\u751fuuid\n# \u548c\nPUT \/items\/4937973d-e349-460a-a6ad-38625125b24a\n<\/code><\/pre>\n<p>\u5c0d\u884c\u52d5\u88dd\u7f6e\u7684 client \u4f86\u8aaa\uff0c\u7528POST\u7684\u554f\u984c\u5728\u65bc\u96e2\u7dda\u74b0\u5883\u7684\u4e0d\u7a69\u5b9a\uff0c\u6709\u53ef\u80fdPOST\u4e4b\u5f8c\u6c92\u6709\u6536\u5230\u56de\u50b3\uff0c\u56e0\u6b64\u884c\u52d5\u88dd\u7f6e\u4e0d\u78ba\u5b9a\u6709\u6c92\u6709\u540c\u6b65\u6210\u529f\uff0c\u9019\u6642\u5019\u8981\u518d\u91cd\u8a66(retry)\uff0c\u4f46\u662f\u7528 POST \u5c31\u7206\u70b8\u4e86\uff0c\u56e0\u70ba server \u6703\u518d\u65b0\u5efa\u4e00\u7b46 uuid \u91cd\u8907\u7684\u8cc7\u6599\u3002\u4f46\u662f\u7528 PUT \u5c31\u6c92\u6709\u554f\u984c\u4e86\uff0cPUT \u662f Idempotent \u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u91cd\u9001\u6c92\u6709\u95dc\u4fc2 (\u53ef\u4ee5\u518d\u642d\u914d Conditional PUT \u7684\u6a5f\u5236\uff0c\u7528 ETag \u548c Last-Modified Headers \u78ba\u4fdd\u6c92\u6709\u8986\u84cb\u885d\u7a81)<\/p>\n<p>\u5982\u679c\u662f\u6c92\u6709 offline \u9700\u6c42\u7684 client\uff0c\u4f8b\u5982\u7b2c\u4e09\u65b9\u61c9\u7528\uff0c\u90a3\u9ebc\u5c31\u53ef\u4ee5\u7528 POST \/items \u9019\u500b API\uff0c\u4ea4\u7531 server \u4f86\u7522\u751f uuid\u3002<\/p>\n<h3>\u5176\u4ed6\u53c3\u8003\u8cc7\u6599<\/h3>\n<ul>\n<li><a href=\"http:\/\/amundsen.com\/blog\/archives\/1122\">POST-PUT-PATCH illustrated<\/a><\/li>\n<li><a href=\"http:\/\/jcalcote.wordpress.com\/2008\/10\/16\/put-or-post-the-rest-of-the-story\/\">PUT or POST: The REST of the Story<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u95dc\u65bc HTTP \u57fa\u672c\u77e5\u8b58\uff0c\u53ef\u4ee5\u53c3\u8003\u6211\u7684 \u7db2\u8def\u6982\u8ad63: HTTP \u5728\u521d\u5b78REST\u7684\u9019\u5e7e\u5e74\uff0c\u6211\u90fd\u8a8d\u70ba\u9019\u5e7e\u500b HTT &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/6483-http-verbs-post-put-patch\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008HTTP Verbs: \u8ac7 POST, PUT \u548c PATCH \u7684\u61c9\u7528\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[34,28],"tags":[],"class_list":["post-6483","post","type-post","status-publish","format-standard","hentry","category-http","category-rest","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-1Gz","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/6483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=6483"}],"version-history":[{"count":29,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/6483\/revisions"}],"predecessor-version":[{"id":12066,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/6483\/revisions\/12066"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=6483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=6483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=6483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}