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
Related
larzurlURL parse/build + query-string encode/decode→Parse JSON in LarzscriptParse a JSON string into real Larzscript dicts and lists, and turn data back into JSON text.→Read and Parse a CSV File in LarzscriptParse CSV text into rows, or straight into dicts keyed by header - including quoted fields with embedded commas.→Encode and Decode Base64 in LarzscriptBase64-encode arbitrary text (or decode it back), the standard way binary-ish data travels safely through text-only channels like JSON or URLs.→