Retrieving parameters from a URL
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Retrieving parameters from a URL usually means reading the query string, the part after ?, such as ?page=2&sort=desc. Modern web APIs make this straightforward, but it still helps to understand the difference between path segments, query parameters, and fragments so you read the right part of the URL.
What Counts as a URL Parameter
Consider this URL:
Its pieces are:
- path:
/products - query string:
category=books&page=2 - fragment:
reviews
When people say "URL parameters," they usually mean the query string key-value pairs:
- '
category=books' - '
page=2'
The fragment after # is not sent to the server in a normal HTTP request, so do not confuse it with query data.
Modern JavaScript: URLSearchParams
In browser JavaScript, the cleanest solution is URLSearchParams.
If the page URL is:
then the output is:
This is the standard answer for modern front-end code.
Getting Multiple Values
Some parameters can appear more than once:
In that case, get() returns only the first value. Use getAll() if you want every one.
Output:
This matters for filter UIs and API designs that allow repeated keys.
Server-Side Example in Python
On the server side, you usually parse the query string from the request URL rather than from window.location.
Here is a standard-library Python example:
Output:
Notice that parse_qs returns lists because repeated parameters are allowed.
Handling Missing Parameters
Parameters are often optional, so code should handle the missing case explicitly.
Or in Python:
Default handling like this keeps your application from crashing or silently producing null-like values in the wrong place.
Query Parameters Are Strings
A subtle but important detail is that query parameters arrive as strings. If you need a number or boolean, parse it yourself.
Do not assume the runtime converts types automatically just because the text looks numeric.
Encoding and Safety
URL parameters are encoded. For example, spaces are often represented as %20 or +. Good parsing APIs decode this for you automatically, which is another reason to avoid manual string splitting when a real URL parser is available.
Also remember that query parameters are untrusted input. Validate them before using them in database queries, file paths, or security-sensitive logic.
Common Pitfalls
One common mistake is parsing the whole URL with ad hoc string operations instead of using built-in URL parsing utilities. That works until encoding, repeated keys, or missing values appear.
Another issue is forgetting that query parameters are strings. "2" and 2 are not the same thing in most programming languages.
Developers also sometimes read the fragment after # and assume it was sent to the server. In normal HTTP requests, it was not.
Finally, repeated parameters can surprise people. If your API allows the same key more than once, choose parsing functions that preserve that structure instead of silently dropping later values.
Summary
- URL parameters usually mean the query string after
?. - In browser JavaScript, use
URLSearchParams. - On the server, use your language's URL parser instead of manual string splitting.
- Query parameters are strings, so convert types explicitly when needed.
- Handle missing keys, repeated keys, and untrusted input carefully.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.