[Answered ]-Nginx, django and x_requested_with: request.is_ajax() returns False

2👍

Try to change the underscores in X_REQUESTED_WITH:XMLHttpRequest with dashes: X-REQUESTED-WITH:XMLHttpRequest.

I had the same problem with nginx as proxy deleted the X_REQUESTED_WITH field from the request. Per default nginx marks headers with underscores as invalid and ignores invalid headers.

You can use nginx directives to either allow underscores in headers with underscores_in_headers on; or don’t ignore invalid header with ignore_invalid_headers off;.

In my concrete case I used python requests as client, django as server and nginx as proxy and solved my problem in which I renamed X_REQUESTED_WITH to X-REQUESTED-WITH. (Django automatically adds 'HTTP_' to the start of x-header names before making the header available in request.META.)

import requests
headers = {'X-REQUESTED-WITH':'XMLHttpRequest'}
r = requests.post(url, data=payload, headers=headers)
👤elim

Leave a comment