I write this little WSGI script :
def application(environ, start_response):
data = '\n'.join(sorted(['%s = %s' % i for i in environ.items()]))
print data
start_response("200 OK", [
("Content-Type", "text/plain"),
("Content-Length", str(len(data)))
])
return iter([data])
I am wondering how can I detect I receive an ajax request : When I run it with gunicorn and when I generate an ajax request with $.ajax() from jQuery, I got this for environ variable:
HTTP_ACCEPT = */*
HTTP_ACCEPT_ENCODING = gzip, deflate, sdch, br
HTTP_ACCEPT_LANGUAGE = fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
HTTP_ACCESS_CONTROL_REQUEST_HEADERS = 16hhs673uh
HTTP_ACCESS_CONTROL_REQUEST_METHOD = GET
HTTP_CONNECTION = keep-alive
HTTP_HOST = localhost:8222
HTTP_ORIGIN = http://localhost:6543
HTTP_REFERER = http://localhost:6543/test2
HTTP_USER_AGENT = Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
PATH_INFO = /test2
QUERY_STRING = name=John4&time=2pm
RAW_URI = /test2?name=John4&time=2pm
REMOTE_ADDR = 127.0.0.1
REMOTE_PORT = 40668
REQUEST_METHOD = OPTIONS
SCRIPT_NAME =
SERVER_NAME = 127.0.0.1
SERVER_PORT = 8222
SERVER_PROTOCOL = HTTP/1.1
SERVER_SOFTWARE = gunicorn/19.7.1
gunicorn.socket = <socket._socketobject object at 0x7f14844e51a0>
wsgi.errors = <gunicorn.http.wsgi.WSGIErrorsWrapper object at 0x7f14844e47d0>
wsgi.file_wrapper = <class 'gunicorn.http.wsgi.FileWrapper'>
wsgi.input = <gunicorn.http.body.Body object at 0x7f14844e4950>
wsgi.multiprocess = False
wsgi.multithread = False
wsgi.run_once = False
wsgi.url_scheme = http
wsgi.version = (1, 0)
I do not see any information like HTTP_X_REQUESTED_WITH, so how Django or Pyramid can known it is an ajax request ?