3

How can I get param from URL after #?

Example:

http://localhost/addAccount/#code=qwerty

I tried use url = request.path and url.spit('/') but it isn't working becasuse request.path don't read string after # in url.

1
  • 3
    The # is for html ids and therefore client side, I don't think they are sent with requests. Commented Mar 1, 2018 at 22:03

3 Answers 3

2

In a URL, what travels after # is known as hash. In an HTTP request that reaches a server (server side) this data does not travel to the server. Therefore, on the server side, it is not possible to retrieve it (web browsers do not send this data in the HTTP request).

However, on the client side it is possible. In Javascript you could do:

window. location. hash

Sign up to request clarification or add additional context in comments.

Comments

0

dont try to parse urls manually - use the stdlib urllib.parse.urlparse
function (urlparse.urlparse on python2):

from urllib.parse import urlparse  # from urlparse import urlparse on py2
scheme = urlparse('http://localhost/addAccount/#code=qwerty')
print(scheme.fragment)

prints out:

code=qwerty

Unfortunately you cannot get from the server-side the fragement of the url (data after the #). AFAIK all browsers wont send the fragement to the server (the fragement can be used only on client side code (e.g. javascript).

Quoting Wikipedia:

When an agent (such as a Web browser) requests a web resource from a Web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the document type and fragment value.[2]

2 Comments

Most common browsers? It should be all browsers, since they're not supposed to send the hash.
one can send a request to a web server containing # in the url and the http spec for url dont disallow # AFAIK inside a url. but it seems more right
0

I had similar thing recently
So I came up with this simple snippet:

from urllib.parse import parse_qs, urlparse


url = "http://localhost/addAccount/#code=qwerty"
fragment = urlparse(url).fragment
value = parse_qs(fragment).get("code")
print(value.pop())  # querty

We had to have "pop" here, as parse_qs returns list by default

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.