Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Get raw request path

Tags:

django

How do I get the raw request path (everything after the host name and the port) in Django?

I tried request.get_full_path(), but it doesn’t work for some URLs.

For example, when the URL is http://localhost:8000/data/?, the result is /data/ instead of /data/?.

I know that the server receives the full string because it show "GET /data/? HTTP/1.1" 200 642 in the terminal.

like image 551
nd97 Avatar asked Oct 24 '25 15:10

nd97


2 Answers

You can use request.build_absolute_uri()

Depends on source code:

Builds an absolute URI from the location and the variables available in this request. If no location is specified, the absolute URI is built on request.get_full_path(). Anyway, if the location is absolute, it is simply converted to an RFC 3987 compliant URI and returned and if location is relative or is scheme-relative (i.e., //example.com/), it is urljoined to a base URL constructed from the request variables.

like image 98
kamilyrb Avatar answered Oct 26 '25 04:10

kamilyrb


Getting access to the raw URL depends on your setup. I'm not aware of a way to do this with Django's development server.

HTTPRequest.META is described as follows:

A dictionary containing all available HTTP headers. Available headers depend on the client and server, but here are some examples:

If you're running gunicorn, request.META["RAW_URI"] might give you what you need.

like image 29
Craig Anderson Avatar answered Oct 26 '25 06:10

Craig Anderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!