Sobhagya Mohanty
Sobhagya Mohanty

Reputation: 628

Not able to parse query parameters with # url

Not able to parse query string parameters of url having #.

http://www.example.com/txn/#/user/399AC2F1BB3241823B5E48205C0657FC?utm_source=campaign&utm_medium=testmedium&page=user

URLEncodedUtils.parse return zero result.

Even URL.getQuery() gives null.

Any method to parse it with explicitly removing # from url.

Upvotes: 5

Views: 1546

Answers (1)

Andreas
Andreas

Reputation: 159096

Everything after # is a fragment.

A URL is built like this (from Wikipedia):

scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

Your URL decomposes to (using URL, scheme is called protocol and fragment is called ref):

scheme   = http
host     = www.example.com
path     = txn/
query    = 
fragment = /user/399AC2F1BB3241823B5E48205C0657FC?utm_source=campaign&utm_medium=testmedium&page=user

Also, if you enter that URL into a web browser, the fragment is handled by the browser and not sent to the server.


If you truly want a # as a path component, it needs to be % encoded:

http://www.example.com/txn/%23/user/399AC2F1BB3241823B5E48205C0657FC?utm_source=campaign&utm_medium=testmedium&page=user

That will decompose (and decode) into:

scheme   = http
host     = www.example.com
path     = txn/#/user/399AC2F1BB3241823B5E48205C0657FC
query    = utm_source=campaign&utm_medium=testmedium&page=user
fragment = 

Once the query has been extracted, you can use URLEncodedUtils.parse. parse is not for the entire URL.

Upvotes: 3

Related Questions