Permalink
Browse files

Added ``X-HTTP-Method-Override`` support, for situations where you ca…

…n't use ``PUT/DELETE/PATCH``. Thanks to maraujop for the original patch!
  • Loading branch information...
toastdriven committed Aug 5, 2012
1 parent 7b8d698 commit 6b8b884e1609d4224619587111e2568adc0fc545
Showing with 28 additions and 0 deletions.
  1. +1 −0 AUTHORS
  2. +11 −0 docs/resources.rst
  3. +4 −0 tastypie/resources.py
  4. +12 −0 tests/core/tests/resources.py
View
@@ -60,6 +60,7 @@ Contributors:
* Julien Bouquillon (revolunet) for authentication and authorization docs fixes.
* Andrey Voronov (eyvoro) for fixing a typo in the AUTHORS file.
* D.B. Tsai (dbtsai) for a fix relating to ``detail_uri_kwargs``.
+* maraujop for a patch adding to ``X-HTTP-Method-Override`` support.
Thanks to Tav for providing validate_jsonp.py, placed in public domain.
View
@@ -713,6 +713,17 @@ filter the queryset before processing a request::
return orm_filters
+Using PUT/DELETE/PATCH In Unsupported Places
+============================================
+
+Some places, like in certain browsers or hosts, don't allow the
+``PUT/DELETE/PATCH`` methods. In these environments, you can simulate those
+kinds of requests by providing an ``X-HTTP-Method-Override`` header. For
+example, to send a ``PATCH`` request over ``POST``, you'd send a request like::
+
+ curl --dump-header - -H "Content-Type: application/json" -H "X-HTTP-Method-Override: PATCH" -X POST --data '{"title": "I Visited Grandma Today"}' http://localhost:8000/api/v1/entry/1/
+
+
``Resource`` Methods
====================
View
@@ -435,6 +435,10 @@ def dispatch(self, request_type, request, **kwargs):
throttling, method lookup) surrounding most CRUD interactions.
"""
allowed_methods = getattr(self._meta, "%s_allowed_methods" % request_type, None)
+
+ if 'HTTP_X_HTTP_METHOD_OVERRIDE' in request.META:
+ request.method = request.META['HTTP_X_HTTP_METHOD_OVERRIDE']
+
request_method = self.method_check(request, allowed=allowed_methods)
method = getattr(self, "%s_%s" % (request_method, request_type), None)
@@ -674,6 +674,7 @@ def test_mangled(self):
class NoteResource(ModelResource):
class Meta:
resource_name = 'notes'
+ authorization = Authorization()
filtering = {
'content': ['startswith', 'exact'],
'title': ALL,
@@ -1954,6 +1955,17 @@ def test_dispatch(self):
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.content, '{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": 1, "is_active": true, "resource_uri": "/api/v1/notes/1/", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}')
+ # Check for an override.
+ request.META = {
+ 'HTTP_X_HTTP_METHOD_OVERRIDE': 'PATCH',
+ }
+ request._read_started = False
+ request._raw_post_data = request._body = '{"title": "Super-duper override ACTIVATE!"}'
+ resp = resource.dispatch('detail', request, pk=1)
+ self.assertEqual(resp.status_code, 202)
+ self.assertEqual(resp.content, '')
+ self.assertEqual(Note.objects.get(pk=1).title, u'Super-duper override ACTIVATE!')
+
def test_build_bundle(self):
resource = NoteResource()

0 comments on commit 6b8b884

Please sign in to comment.