Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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
You can’t perform that action at this time.