RESTful, 说说 http 的 patch method

最早的时候,我们只需要 GET 和 POST 方法,POST 方法的引入也只是为了消除 URL 过长,参数隐藏,上传文件的问题,完全和语义无关。接触到 RESTful 之后,我们开始思考 GET 和 POST 的不同语义,并且十分必要的去发掘出所有的 HTTP method,HTTP/1.1 所实现的 method,见 RFC 2616, 有这些:

OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT

规范是这么定义的,这还要看容器实现了多少,比如 Tomcat 7 中的 servlet api 实现了

doOptions, doGet, doHead, doPost, doPut, doDelete, doTrace 就差个 doConnect 了。

而我们这里要说的 PATCH method 是在 Servlet 3.0 和当前 Tomcat 7 中都提到的,也就是尚未实现它。

这也难怪,PATCH 在 2010 年三月份才成为正式的方法,见 RFC 5789。没有 PATCH 的时候我们进行更新的操作采用的是 PUT 方法。那么 PATCH 和 PUT 有什么区别呢?

同样可以从语义上去理解,有两方面的对比:

1. 对已有资源的操作:

PATCH 用于资源的部分内容的更新,例如更新某一个字段。具体比如说只更新用户信息的电话号码字段

而 PUT 用于更新某个资源较完整的内容,比如说用户要重填完整表单更新所有信息,后台处理更新时可能只是保留内部记录 ID 不变。

2. 当资源不存在时:

联想到版本控制 PATCH 是修改原来的内容,也可能会产生一个新的版本。比如当资源不存在的时候,PATCH 可能会去创建一个新的资源,这个意义上像是 saveOrUpdate 操作。

本文原始链接 http://unmi.cc/restful-http-patch-method/, 来自 隔叶黄莺 Unmi Blog

PUT 只对已有资源进行更新操作,所以是 update 操作

见 When should we use the PATCH HTTP method? in The RESTful CookBook

The HTTP methods PATCH can be used to update partial resources. For instance, when you only need to update one field of the resource, PUTting a complete resource representation might be cumbersome and utilizes more bandwidth

PATCH /user/jthijssen HTTP/1.1
<user>
    <firstname>Joshua</firstname>
</user>

Also, the PUT method is idempotent. PUTting the same data multiple times to the same resource, should not result in different resources, while POSTing to the same resource can result creation of multiple resources.

- See more at: http://restcookbook.com/HTTP%20Methods/patch/#sthash.gYGM7j3Q.dpuf

在 RFC 5789 更详述了 PATCH 与 PUT 的区别。

再来看下目前谁实现了 PATCH 方法,谁还没有实现 PATCH 方法

1. Apache HttpComponents HttpClient version 4.2 or later 支持了 PATCH
2. 目前 JDK7 的 HttpURLConnection 未实现 PATCH
3. TOMCAT 7 也不行
4. PlayFramework 2 也不支持
5. Spring 3.2 开始支持 PATCH 方法,但要选对部署的容器
6. JBoss Netty 支持 PATCH,可见: http://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/codec/http/class-use/HttpMethod.html

总之现在实现了 PATCH 方法的容器真是少。

参考:1. List of HTTP methods (verbs)             2. Method Definitions             3. RFC 5789 - PATCH Method for HTTP             4. RFC 2616             5. Standard Methods- RESTful API Design

本文链接 http://unmi.cc/restful-http-patch-method/, 来自 隔叶黄莺 Unmi Blog

时间: 2025-01-11 19:11:51

RESTful, 说说 http 的 patch method的相关文章

HTTP POST, PUT PATCH

POST = 新增 GET = 讀取 PUT = 更新 DELETE = 刪除 PUT 会在地址栏显示参数信息,不安全! 理解POST和PUT的区别,顺便提下RESTfu 这两个方法咋一看都可以更新资源,但是有本质区别的 具体定义可以百度,我这里就不贴了,光说我自己的理解 首先解释幂等,幂等是数学的一个用语,对于单个输入或者无输入的运算方法,如果每次都是同样的结果,则称其是幂等的 对于两个参数,如果传入值相等,结果也等于每个传入值,则称其为幂等的,如min(a,b) POST 用于提交请求,可以

[Express + Webstrom] Debug Node.js RESTful application

Using WebStrom can easily debug the Node applcation. For example, we have an Node+Express application. server.js: /** * Created by Answer1215 on 12/9/2014. */ 'use strict'; var expres = require('express'); var app = expres(); app.get('/', function(re

2014/08/13 – Backbonejs

[来自: Backbone.js 开发秘笈 第7章] Restful 服务调用 Collection.fetch() - 请求集合 Model.save() - 新增或修改模型(根据 Model.isNew() 方法判断操作类型) Model.destroy() - 删除模型 Model.sync() - 请求的执行方法, fetch(), save(), destroy() 都调用其方法进行操作请求 (function ($) { //define ----------------------

a REST API

https://spring.io/guides/tutorials/bookmarks/ http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven I am getting frustrated by the number of people calling any HTTP-based interface a REST API. Today's example is the SocialSite REST AP

个人对drf-extentions 的英文文档的部分整理翻译与保存(个人用)

Caching To cache something is to save the result of an expensive calculation so that you don't have to perform the calculation next time. Here's some pseudocode explaining how this would work for a dynamically generated api response: given a URL, try

对一个前端AngularJS,后端OData,ASP.NET Web API案例的理解

依然chsakell,他写了一篇前端AngularJS,后端OData,ASP.NET Web API的Demo,关于OData在ASP.NET Web API中的正删改查没有什么特别之处,但在前端调用API时,把各种调用使用$resouce封装在一个服务中的写法颇有借鉴意义. 文章:http://chsakell.com/2015/04/04/asp-net-web-api-feat-odata/源码:https://github.com/chsakell/odatawebapi 首先是领域模

OpenStack Identity API v3

Table Of Contents OpenStack Identity API v3 What’s New in Version 3.7 What’s New in Version 3.6 What’s New in Version 3.5 What’s New in Version 3.4 What’s New in Version 3.3 What’s New in Version 3.2 What’s New in Version 3.1 What’s New in Version 3.

[转]Calling an OData Service From a .NET Client (C#)

本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v3/calling-an-odata-service-from-a-net-client by Mike Wasson+ Download Completed Project+ This tutorial shows how to call an OData service from a C# c

{ICIP2014}{收录论文列表}

This article come from HEREARS-L1: Learning Tuesday 10:30–12:30; Oral Session; Room: Leonard de Vinci 10:30  ARS-L1.1—GROUP STRUCTURED DIRTY DICTIONARY LEARNING FOR CLASSIFICATION Yuanming Suo, Minh Dao, Trac Tran, Johns Hopkins University, USA; Hojj