简体   繁体   中英

Django - Get raw request path

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.

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.

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 (ie, //example.com/ ), it is urljoined to a base URL constructed from the request variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM