Check if request is AJAX in Python
Asked Answered
S

5

18

Is there a way to check if a request is AJAX in Python?

The equivalent of PHP's $_SERVER['HTTP_X_REQUESTED_WITH'] == 'xmlhttprequest'?

Semicircle answered 14/12, 2011 at 17:5 Comment(0)
C
35

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.

Cyd answered 14/12, 2011 at 17:17 Comment(6)
in Django you can simply do request.is_ajax()Croquet
Thanks @maraujop, that's much easier! I've updated my answer to match.Cyd
can't this header be spoofed?Canales
This header can easily be spoofed, so use with caution. Also the is_ajax() method is deprecated as of Django version 3.1.Franco
is_ajax() is deprecated in django 3.2 and forward as of 28th May, 2021.Vidal
HttpRequest.is_ajax() is in fact deprecated since Django 3.1. Have a look to the changelog here. The changelog gives us the solution : check if request.headers.get('x-requested-with') == 'XMLHttpRequest'Byrd
V
4

is_ajax() is deprecated since Django 3.1 (as of 28th May, 2021) as they stated that it depends on jQuery ways of sending request but since people use fetch method a lot to make call Ajax calls, it has become unreliable in many cases.

Usually, text/html is requested in the header in the first option in the browser when it just the loads the page. However, if you were making API call, then it would become */* by default, if you attach Accept: application/json in the headers, then it become

application/json

So, you can check easily if the request is coming from ajax by this

import re # make sure to import this module at the top

in your function

requested_html = re.search(r'^text/html', request.META.get('HTTP_ACCEPT'))
if not requested_html:
    # ajax request it is
Vidal answered 28/5, 2021 at 7:57 Comment(0)
B
0

Check this how to check if request is ajax in turbogears

also this for how to do this in Django(it depends in your framework): How to check contents of incoming HTTP header request

Brainsick answered 14/12, 2011 at 17:27 Comment(0)
J
0

Just in case someone can find this useful, the Sec-Fetch-Dest can hint if the request is an ajax call or not.

So one could have something like this:

is_ajax_call = all(
                    request.headers.get("Sec-Fetch-Dest", "")
                    not in secFetchDest for secFetchDest in ["document", "iframe"]
                )
Jassy answered 3/3, 2022 at 11:53 Comment(0)
B
0

Generally speaking, check if the request header attribute x-requested-with is equal to XMLHttpRequest.

From Django 4.0 :

def is_ajax(request):
    return request.headers.get('x-requested-with') == 'XMLHttpRequest'

def view(request):
    if is_ajax(request):
        ...

Source : Django 3.1 changelog

Byrd answered 27/5, 2024 at 8:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.