我有一个基于djangoappengine,Backbone.js和Django REST Framework的应用程序,它使用PATCH请求在{patch: true}
调用时通过model.save
更新模型。
我发现在本地测试时,dev_appserver返回:
ERROR 2014-02-19 04:37:04,531 dev_appserver.py:3081] code 501, message Unsupported method ('PATCH')
INFO 2014-02-19 04:37:04,532 dev_appserver.py:3090] "PATCH /api/posts/5707702298738688 HTTP/1.1" 501 -
然而,当我部署它并通过appspot访问它时,服务器很乐意接受请求。每当我做出改变并希望对其进行测试时,这就迫使我进行部署。
我正在运行Python SDK的最新版本(1.89),并且发现old fixed issue似乎解决了它,但似乎other people have had it。
我试过this patch,但没有什么区别。我不明白为什么开发服务器拒绝它们而不是生产服务器,我需要改变什么?
感谢。
答案 0 :(得分:0)
要更新资源,可以使用POST和x-http-method-override进行修补。这是一个有效的RESTful操作,使用POST将与防火墙和旧用户代理更兼容。请求中的数据应指明要更新的内容。
var url = '/api/posts/5707702298738688'
var patch_ops = [
{ "op": "replace", "path": "/properties/", "author": text}
{ "op": "add", "path": "/replies/", {"author": text, "comment":"blah"}}
/*
{ "op": "remove", "path": "/a/b/c" },
{ "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] },
{ "op": "replace", "path": "/a/b/c", "value": 42 },
{ "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
{ "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }
*/
];
var xhr = jQuery.ajax({
type: "POST",
beforeSend: function (request)
{
request.setRequestHeader("X-HTTP-Method-Override", "PATCH");
},
url: url,
data: my_json_string,
dataType:"json",
success: function(data) {
return data;
},
error: function(xhr, textStatus, error){
return error;
}
});
服务器处理程序:
def post(self, object_name):
if self.request.headers['x-http-method-override'] == 'PATCH':
# update according to patch operations
patch_ops_str= self.request.body.decode('utf-8')
try:
patch_ops = json.loads(new_area_geojson_str)
except:
self.response.set_status(400)
return self.response.out.write('{"status": "error", "reason": "JSON Parse error" }')
else:
self.response.set_status(405)
return self.response.out.write('{"status": "error", "reason": "post not accepted without x-http-method-override to PATCH" }')