Skip to content

std:url ​

URL parsing.

Import ​

pleno
import { Url } from "std:url";
Run

URL is the same namespace under another name: import { URL } from "std:url";.

Functions ​

parse(url: String): Map ​

Parses an absolute URL string and returns its components. Throws an error for an invalid or relative URL.

pleno
import { Url } from "std:url";

let url = Url.parse("https://user:pass@example.com:8080/path?q=1#section");
print(url.port);  // 8080
print(url.query); // q=1
Run

Returns:

PropertyTypeExample
hrefString"https://user:pass@example.com:8080/path?q=1#section"
protocolString"https"
usernameString"user" (empty string if none)
passwordString?"pass"
hostString?"example.com"
portInt?8080 (null when it is the default for the scheme)
pathString"/path"
queryString?"q=1"
hashString?"section"

Missing optional parts are null.

Example:

pleno
import { Url } from "std:url";

let url = Url.parse("https://api.example.com/v1/users");
print(url.protocol);  // https
print(url.host);      // api.example.com
print(url.path);      // /v1/users
print(url.port);      // null

try {
    Url.parse("not a url");
} catch (e) {
    print(e.message); // Invalid URL: relative URL without a base
}
Run