Passing data in the URL
You met the query string in lesson 1-2: everything after the ?, as key=value pairs joined by &.
curl "https://api.example.com/search?q=coffee&page=2"(Quote the URL in a shell, otherwise the shell treats & as its own operator.)
One complication: URLs only allow a limited set of characters. Spaces, accents, and symbols like & inside a value would break the structure. The fix is percent-encoding: each forbidden byte becomes % plus its hex value. A space becomes %20 (or + in query strings), é becomes %C3%A9, and a literal & in a value becomes %26.
Encoding bugs are a classic source of quiet failures: an unencoded & inside a search term silently splits it into two parameters, and the API "works" while filtering on garbage. So the rule is: you never encode by hand. Every language has a helper, and it is a one-liner in Python:
Code exercise · python
Run this. urlencode takes a dict of parameters and produces a correctly encoded query string. Notice the spaces in the values become +.
Code exercise · python
Your turn, be the server. Parse the raw query string into its pairs: split on & to get each pair, split each pair on = to get key and value, and print them as shown.
Quiz
A URL contains the path /files/annual%20report.pdf. What is the file actually called?