2

我已經有了基於djangoappengine一個應用程序,Backbone.js的和Django的REST框架使用PATCH請求通過{patch: true}model.save調用款式更新。dev_appserver.py本地拒絕PATCH請求,但接受它部署

我發現,在本地測試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的latests版本(1.89),並發現和old fixed issue這似乎解決它,但它似乎other people have had it

我試過this patch但它沒有區別。我不明白爲什麼開發服務器會拒絕他們而不是生產服務器,是否有我需要改變的東西?

謝謝。

+0

這固定的問題涉及在URL中的PATCH方法獲取服務,而不是開發Web服務器調整。 [問題975](https://code.google.com/p/googleappengine/issues/detail?id=975)表示PROPPPATCH可以修復WebDAV支持,並且開發Web服務器上仍不支持PATCH :-( –

回答

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": "https://stackoverflow.com/a/b/c" }, 
      { "op": "add", "path": "https://stackoverflow.com/a/b/c", "value": [ "foo", "bar" ] }, 
      { "op": "replace", "path": "https://stackoverflow.com/a/b/c", "value": 42 }, 
      { "op": "move", "from": "https://stackoverflow.com/a/b/c", "path": "https://stackoverflow.com/a/b/d" }, 
      { "op": "copy", "from": "https://stackoverflow.com/a/b/d", "path": "https://stackoverflow.com/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" }') 

Please do not patch like an idiot