get the referer url in python/webapp2 for a post request

user993563 picture user993563 · Jun 30, 2012 · Viewed 10.3k times · Source

I want to get the referer url that has sent in the request to the server. I am using the webapp2 framework on appengine.

when i do:

def post(self, slug):
    print self.request 

i get the following output:

OST /first/person/ HTTP/1.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Accept-Language: en-US,en;q=0.8 Cache-Control: max-age=0 Connection: keep-alive Content-Length: 43 Content-Type: application/x-www-form-urlencoded Content_Length: 43 Content_Type: application/x-www-form-urlencoded Cookie: djdt=hide; csrftoken=09f29b2bfe9f51f16581bab7fe7c6f38; sessionid=49b995d954eae8d613cedc3f4e92a796 Host: 127.0.0.1:8080 Origin: http://127.0.0.1:8080 Referer: http://127.0.0.1:8080/person/1/ 

as is clearly visible there is Referer: http://127.0.0.1:8080/person/1/

i am interested in only the referer, hence i tried:

referer = self.request.headers['Referer']
            print referer

which gives the output:

False Status: 200 Content-Type: text/html; charset=utf-8 Content-Length: 0 Cache-Control: no-cache

One way of getting would be to do string processing on request, but i am sure there would be something more standard.

Answer

apcelent picture apcelent · Jul 2, 2012

As mentioned in your comment, this happens in webapp2, however if you want to redirect the user to the location from where the request has come, it can be as simple as:

webapp2.redirect(self.request.referer)