Data formats · real, tested code

Parse a URL in Larzscript

Break a URL into its scheme, host, path and query parameters, and safely join a base URL with a relative path.

url.urljoin() specifically avoids the double-slash/missing-slash bugs manual string concatenation invites when combining a base URL with a path.

larzscript pkg install url

Code

import "url" as url

let u = url.parse("https://example.com/path?a=1&b=two words")
print(u["scheme"], u["host"], u["path"], u["query"]["b"])
print(url.urljoin("https://example.com/api/", "users"))
print(url.urljoin("https://example.com/api/", "/users"))
Output
https example.com /path two words
https://example.com/api/users
https://example.com/users