URLs and Query Strings

URLs and Query Strings

The URL layer implements WHATWG-style parsing — the same behaviour browsers apply — so URLs extracted from documents resolve and normalize the way they would in a real user agent.


Parsing and Validating

URL.parse() parses an absolute or relative URL; URL.can_parse() answers validity without constructing an object. Parse failures raise the typed URLParseError.

Query Strings

URLSearchParams reads and edits query parameters: append(), get(), get_all(), has(), and delete() cover the round-trip, preserving multi-value semantics.


Tips and Best Practices

  • Use can_parse() for cheap validation in filters; construct with parse() only when you need the components
  • Remember query keys repeat — use get_all() when a parameter may appear more than once
  • Catch URLParseError rather than pre-validating with regular expressions

Common Issues

IssueCauseFix
Relative URL fails to parseNo base providedSupply the base per URL.parse()
Only first query value returnedUsed get() on a repeated keyUse get_all()
Odd characters in componentsWHATWG normalization at workExpected — this matches browser behaviour

FAQ

Does parsing follow the WHATWG standard?

Yes — normalization and component splitting mirror browser behaviour.

How do I check a URL without parsing it?

URL.can_parse() returns validity without object construction.

Can I edit a query string in place?

Yes — URLSearchParams supports append/get/has/delete round-trips.


API Reference Summary

Class/MethodDescription
URL.parseParse a URL
URL.can_parseValidity check without construction
URLSearchParams.appendAdd a query parameter
URLSearchParams.get_allAll values for a key
URLParseErrorTyped parse failure

See Also