Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the request origin in a Django request

So I'm trying to enable cross origin resource sharing in Django, so I can post to an external site, and it's easy to do when I set

response["Access-Control-Allow-Origin"]="*" 

but I want to instead have it check whether the origin is in an allowed list of origins (essentially to restrict it to only allow specific sites) but I can't seem to find anywhere in the Django request where I can get the origin information.

I tried using request.META['HTTP_HOST'] but that just returns the site that's being posted to. Does anyone know where in the Request object I can get the origin of the request?

like image 727
Jared Joke Avatar asked Jan 16 '14 17:01

Jared Joke


3 Answers

As for getting the url from request (which is what I was looking for), use request.META['HTTP_REFERER'] instead.

like image 150
ZAD-Man Avatar answered Nov 17 '22 13:11

ZAD-Man


In Django,

request.headers['Origin']

answers the original question.

You can print(request.headers) to see everything available in the headers.

like image 9
Joel Wigton Avatar answered Nov 17 '22 13:11

Joel Wigton


Use this:

origin = request.META.get("HTTP_ORIGIN")

This is the way django-cors-headers use it in the middleware:

like image 1
Omid Raha Avatar answered Nov 17 '22 15:11

Omid Raha