Check if request is AJAX in Python

ajax, http-headers, python, xmlhttprequest

Solution

If the AJAX framework sets the X-Requested-With header in its requests, then you will be able to use that header to detect AJAX calls. It's up to the client-side framework to do this.

Getting hold of the HTTP headers depends on your Python framework of choice. In Django, the `request` object has an `is_ajax` method you can use directly.

Problem

Is there a way to check if a request is AJAX in Python? The equivalent of PHP's `$_SERVER['HTTP_X_REQUESTED_WITH'] == 'xmlhttprequest'`?

Original source

Related problems