URL

The URL module provides functions for parsing URLs and encoding/decoding URL components.

Parsing

URL.parse
String -> Result {scheme, host, port, path, query, fragment} String
Parses a URL string into its components.
match URL.parse "https://example.com:8080/path?q=1#section"
  | Ok url ->
      println "Scheme: ${url.scheme}"
      println "Host: ${url.host}"
      println "Port: ${url.port}"
      println "Path: ${url.path}"
      println "Query: ${url.query}"
      println "Fragment: ${url.fragment}"
  | Err msg -> println "Parse error: ${msg}"
# => Scheme: https
# => Host: example.com
# => Port: 8080
# => Path: /path
# => Query: q=1
# => Fragment: section
URL.to-string
{scheme, host, ...} -> String
Converts a URL record back to a string.
url = {scheme: "https", host: "example.com", port: 443, path: "/api", query: "", fragment: ""}
println (URL.to-string url)
# => "https://example.com/api"

Encoding and Decoding

URL.percent-encode
String -> String
Encodes special characters using percent-encoding for use in URLs.
println (URL.percent-encode "hello world")
# => "hello%20world"

println (URL.percent-encode "name=John&city=NYC")
# => "name%3DJohn%26city%3DNYC"
URL.percent-decode
String -> String
Decodes percent-encoded characters in a URL string.
println (URL.percent-decode "hello%20world")
# => "hello world"

println (URL.percent-decode "name%3DJohn%26city%3DNYC")
# => "name=John&city=NYC"