Best way to parse URL string to get values for keys?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Extracting query parameter values from a URL is one of the most common tasks in web development. Every major language provides built-in URL parsing utilities that handle encoding, edge cases, and multi-valued parameters correctly. Avoid writing custom split-and-loop parsers when standard libraries exist.
JavaScript (Browser and Node.js)
The URL and URLSearchParams APIs work in all modern browsers and Node.js 10+.
URLSearchParams automatically decodes percent-encoded values and handles + as space.
Python
Python's urllib.parse module provides urlparse and parse_qs for reliable URL decomposition.
Use parse_qs (returns lists) rather than parse_qsl (returns flat tuples) when you need multi-valued parameter support.
PHP
PHP's parse_url and parse_str handle URL decomposition.
Go
Go's net/url package parses URLs and query parameters into a Values map.
Query() returns url.Values, which is map[string][]string. Use Get() for the first value or index the map directly for all values.
C# / .NET
Use System.Uri with HttpUtility.ParseQueryString or the newer QueryHelpers in ASP.NET Core.
Handling Edge Cases
Several URL parsing pitfalls apply across all languages:
Common Pitfalls
- Writing manual
split("&")andsplit("=")parsers that break on encoded=or&characters inside values. - Forgetting that
parse_qsreturns lists, not single values, and accessing the result as a string. - Confusing URL fragment (
#section) with query parameters — fragments are never sent to the server. - Not handling multi-valued parameters (same key appearing multiple times like
?tag=a&tag=b). - Assuming query parameter order is guaranteed — it is not part of the URL specification.
Summary
- Use
URLSearchParamsin JavaScript,parse_qsin Python,parse_url/parse_strin PHP,url.Parsein Go, andHttpUtility.ParseQueryStringin C#. - Standard libraries handle percent-encoding,
+spaces, and multi-valued parameters correctly. - Always use
getwith a default value for optional parameters. - Never write custom string-splitting parsers for URL query strings in production code.
- Remember that
parse_qsreturns lists — index[0]for single-value access.

