2

我有一个基于 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 -

然而,当我部署它并通过 apppot 访问它时,服务器会愉快地接受请求。这迫使我每次进行更改并想要对其进行测试时都进行部署。

我正在运行 Python SDK 的最新版本(1.89),并发现了似乎可以解决它但似乎其他人已经解决的旧问题

我尝试了这个补丁,但没有任何区别。我不明白为什么开发服务器会拒绝它们而不是生产服务器,我需要改变什么吗?

谢谢。

4

1 回答 1

0

要更新资源,您可以使用带有 x-http-method-override 的 POST 来修补。这是一个有效的 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" }')

改编自请不要像白痴一样修补

于 2015-12-08T04:39:36.083 回答