我们在调用RESTFul服务的时候,有的时候,第三方的服务会提供支持PATCH 操作的方法,在这种情况下,我们如果我们下面的这种方式,
去调用PATCH操作,肯定会返回40X的错误。
PATCH /tasks/v1/users/@me/lists/MTIxNzM1OTkzNjgxNTQ1OTcwNTc6MDow HTTP/1.1 content-type: application/json; charset=UTF-8 connection: close accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */* authorization: Bearer ya29.LgALd08g5-NIQxwAAAB111tdMC-Q-jXhMoRxYEUS107_bD9p6xdVEq9tht3dpQ accept-encoding: iso-8859-1,*,utf-8 x-http-method-override: PATCH accept-charset: utf-8 User-Agent: Jakarta Commons-HttpClient/3.0.1 Host: www.googleapis.com:443 Content-Length: 45 { "title":"This is a test for patch 5555" }
那么,解决的方案是什么呢?发送的时候,我们还是用POST方法,只不过我们在HTTP的消息头里面加上一个属性:x-http-method-override
然后把这个属性的值设置成“PATCH”,然后就可以了,请看下面的测试的请求和结果。
(1)测试请求
(2) 测试返回的结果,说明Patch操作的修改成功了!!!
{ "kind": "tasks#taskList", "id": "MTIxNzM1OTkzNjgxNTQ1OTcwNTc6MDow", "etag": "\"esSgSc9hKkvJ92xs_2jT5Am6L4c/KKY36xrOZKKq3hKD4zW4yCwNmJo\"", "title": "This is a test for patch 5555", "updated": "2014-06-13T08:31:24.000Z", "selfLink": "https://www.googleapis.com/tasks/v1/users/@me/lists/MTIxNzM1OTkzNjgxNTQ1OTcwNTc6MDow" }
RESTFul中的那些事(2)----如何支持RESTFul的HTTP Patch方法?
时间: 2024-10-23 11:52:46