Parse_url equivalent in grails / groovy?

Is there groovy / grails equivalent to PHP parse_url ( http://php.net/manual/en/function.parse-url.php ) or urlparse python ( http://docs.python.org/library/urlparse.html ) which turns the URL string into a structure containing the host, protocol, request sequence, fragment, URI, etc.

I thought it could be in grails.org/doc/latest/api/org/codehaus/ groovy / grails / web / util / WebUtils.html, but I didn't see anything. I donโ€™t think that HTTPBuilder or various URLMapping utilities is what I need.

I just want to get the map out of the path and queryString and handle the edges correctly (array params /blah/fuzz?foo=bar&foo=baz , fragments /blah/fuzz?foo=bar#baz , encoded URLs for redirection).

I know that I can process the PATH component by cleverly using URLMapping, for example: /blah/$code , but im left with the decryption of the parameter block ...

thanks

+6
source share
2 answers

If I understand correctly, you really need a simple old URI class:

 new URI('http://google.com/?q=URL').query 
+7
source

@Artur Nowak answer extension, you may need even more effort to get what you want. Here is an example:

 URI dbUri = new URI('http://google.com/?q=URL') def username = dbUri?.getUserInfo()?.split(":")?.getAt(0) def password = dbUri?.getUserInfo()?.split(":")?.getAt(1) def host = dbUri?.getHost() def databaseInstance = dbUri?.getPath() def url = "jdbc:mysql://" + host + databaseInstance 
+2
source

Source: https://habr.com/ru/post/892713/


All Articles