What You'll Learn
- The seven parts of a URL
- How APIs use the path to identify resources
- How APIs use query parameters to filter, sort, and paginate
- How to URL-encode special characters
Why This Matters
The URL is the address of an API resource. Get any part wrong — a missing slash, an unencoded space, a wrong port — and your request fails. Understanding URL anatomy is the difference between guessing at API errors and reading them precisely.
The Seven Parts of a URL
https://api.example.com:443/v1/users/123?active=true&sort=name#section
\___/ \______________/\__/\__________/\____________________/\______/
scheme host port path query string fragment
| Part | Example | What it does |
|---|---|---|
| Scheme | https | The protocol. Almost always https for production APIs. |
| Host | api.example.com | The domain name of the server. |
| Port | :443 | Optional. Defaults: 443 for HTTPS, 80 for HTTP. Usually omitted. |
| Path | /v1/users/123 | Identifies the specific resource. The most important part for REST APIs. |
| Query string | ?active=true&sort=name | Optional key-value pairs after ?. Used for filtering, sorting, pagination. |
| Fragment | #section | Optional. Browser-only; never sent to the server. Ignore for APIs. |
The Path — Identifying Resources
In a REST API, the path identifies what you're interacting with. Each segment usually represents a resource or a resource collection:
/users → all users (collection)
/users/123 → user with ID 123 (single resource)
/users/123/posts → all posts by user 123 (nested collection)
/users/123/posts/456 → post 456 by user 123
Path segments are separated by /. The last segment may be a collection (plural noun) or a specific resource ID.
Versioning in the path
Many APIs include a version prefix:
/v1/users/123
/v2/users/123
This lets the API introduce breaking changes in /v2/ without breaking existing /v1/ clients.
Query Parameters — Filtering, Sorting, Pagination
Query parameters appear after ? and are separated by &. They modify how the server returns the resource.
GET /users?role=admin&active=true&sort=name&page=2&limit=20
This request means: "give me page 2 of active admin users, sorted by name, 20 per page."
| Parameter | Purpose |
|---|---|
role=admin | Filter — only users with role=admin |
active=true | Filter — only active users |
sort=name | Sort — alphabetical by name |
page=2 | Pagination — page 2 of results |
limit=20 | Pagination — 20 items per page |
URL Encoding — Special Characters
Some characters have special meaning in URLs (?, &, =, #, /, spaces). If you need to include them in a value, you must URL-encode them:
| Character | Encoded as |
|---|---|
| Space | %20 or + |
& | %26 |
= | %3D |
? | %3F |
# | %23 |
/ | %2F |
Example — searching for "Anita & Ravi"
GET /search?q=Anita%20%26%20Ravi
Without encoding, the & would be parsed as a separator between two query parameters, breaking the search.
In code, use the language's URL encoder:
// JavaScript
encodeURIComponent("Anita & Ravi") // "Anita%20%26%20Ravi"
# Python
from urllib.parse import quote
quote("Anita & Ravi") # "Anita%20%26%20Ravi"
Common Mistakes
- Forgetting to encode special characters. Spaces, ampersands, and equals signs in values break the URL.
- Using uppercase in paths when the API expects lowercase. Paths are technically case-sensitive.
/Usersand/usersare different URLs. - Putting authentication tokens in the URL. URLs are logged everywhere. Use the
Authorizationheader instead. - Using query parameters for resource identification.
/users?id=123is wrong;/users/123is right. Use the path for ID, query for filters. - Trailing slash inconsistency.
/usersand/users/are different URLs. Pick one and stick with it.
Practical Exercise (5 minutes)
Break down this URL into its seven parts:
https://api.open-meteo.com/v1/forecast?latitude=19.07&longitude=72.87¤t=temperature_2m
Answers:
- Scheme:
https - Host:
api.open-meteo.com - Port: (default 443, omitted)
- Path:
/v1/forecast - Query string:
latitude=19.07&longitude=72.87¤t=temperature_2m - Fragment: none
Mini Challenge
Construct a URL that asks the JSONPlaceholder API for the first 5 posts by user ID 1, sorted by ID descending. (Hint: JSONPlaceholder supports ?userId=1 for filtering. It does not support pagination or sort parameters, so construct what you can and observe what the API ignores.)
Key Takeaways
- A URL has 7 parts: scheme, host, port, path, query, fragment. For APIs you mostly care about path and query.
- The path identifies the resource (
/users/123). - The query string modifies how the resource is returned (filters, sort, pagination).
- Always URL-encode special characters in values.
- Never put secrets in URLs — use the
Authorizationheader.
Previously: Lesson 07 covered the body.
Today: You learned the URL — the address that every request is sent to.
Next: In lesson 09 — Path Parameters vs Query Parameters, you'll learn when to use each.
FAQ
Why are URLs case-sensitive?
The host is case-insensitive (API.Example.com = api.example.com). But the path and query are technically case-sensitive per RFC 3986. In practice, most APIs treat paths as lowercase, but you should not rely on case-insensitive matching.
What is the maximum URL length?
The spec says URLs can be any length, but browsers and servers impose limits. Typical: 2000 characters for browsers, 8KB for most servers. If your URL gets longer, you're probably sending too much data as query parameters — use a POST body instead.
Comments
Comments
Post a Comment