Skip to content

Instantly share code, notes, and snippets.

@mjhea0
Created August 31, 2014 22:54
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mjhea0/43d7b4285c59c2083123 to your computer and use it in GitHub Desktop.

With POST Tunneling, you are simply making a POST request act like a different type of request. Why is this neceesary? Because if you're using AJAX, most browsers only allow you to send GET or POST requests.

What needs to change?

  1. The headers sent with the request in the JavaScript file: headers: {'X_METHODOVERRIDE': 'DELETE'}
  2. Django middleware. So that the method is updated for requests that contain the X_METHODOVERRIDE header.

Source.

$.ajax({
type: 'POST',
url: 'some_endpoint/',
data: {'your'': 'date'},
headers: {'X_METHODOVERRIDE': 'DELETE'},
success: function(){ console.log("success"); }
error: function(){ console.log("error"); }
});
class TunnelingMiddleware(object):
def process_request(self, request):
if request.META.has_key('HTTP_X_METHODOVERRIDE'):
http_method = request.META['HTTP_X_METHODOVERRIDE']
if http_method.lower() == 'put':
request.method = 'PUT'
request.META['REQUEST_METHOD'] = 'PUT'
if http_method.lower() == 'delete':
request.method = 'DELETE'
request.META['REQUEST_METHOD'] = 'DELETE'
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment