The API and the table
After this: Write the two endpoints with their status codes, and the table with the short code as its key.
On this page · 6 sections · 3 checks
The service has two jobs from lesson 1: make a short link, and send people on when they open one. A browser or a phone app cannot call a function inside the service. It sends a request over the network, as in the first tutorial of this learning path, and reads the response. So what exactly does it send, and what comes back?
Two endpoints
The list of requests a service accepts, and what it answers to each, is its . One kind of request, a method and a path, is an . This service needs two:
| Endpoint | The request carries | The answer |
|---|---|---|
POST /links | the long URL | 201 Created and the new short code |
GET /{code} | the code, in the path | 302 Found and the long URL in a Location header |
POST is the method for a request that creates something. GET is the method for a request that only reads. In GET /{code}, the {code} stands for any code, so GET /21 asks for the link with code 21.
Making a link
Here is what travels for one new link. The first part is the request, the second the response.
POST /links{"url": "https://example.com/recipes/lemon-cake"}201 Created{"code": "21", "short": "https://short.example/21"}A is the three-digit number at the start of every response. 201 Created says that something new was made. The body below it gives the code and the whole short link, so the app can show it to the user.
Following a link
When someone opens short.example/21, the browser sends a GET, and the answer is a redirect:
GET /21302 FoundLocation: https://example.com/recipes/lemon-cake302 Found says the page is somewhere else. The line under it is a : a named line of extra information. Location tells the browser where to go, and the browser opens that address by itself. If no link has the code, the answer is 404 Not Found.
The table
Every link is one row in one table. The Databases and SQL path showed how to choose a key; here the choice is short.
CREATE TABLE links ( code TEXT PRIMARY KEY, long_url TEXT NOT NULL, created_at TEXT NOT NULL);codeis the primary key. Every redirect looks a link up by its code, and the key makes that look-up fast, because SQLite keeps an index on it (see Indexes). The key also makes the database refuse a second row with the same code.long_urlis where the redirect goes. It is not unique: two people may shorten the same page and get two codes.created_atis when the link was made, so links older than five years can be found and removed.
A redirect runs one query. Press Run: the table below starts with three links, coded 21, 22 and 23.
Change '21' to '24' and run again. No row comes back, and that is when the service answers 404.
Check yourself
A browser sends GET /21, and the code 21 is in the table. Which answer sends the browser on to the long URL?
The table links has the columns code, long_url and created_at. Which one is the primary key?
A browser sends GET /zz9. The query SELECT long_url FROM links WHERE code = 'zz9' returns no rows. What does the service answer?
Honesty notes
- Name the two endpoints: POST /links to make a link, answered with 201 Created, and GET /{code} to follow one.
- Answer a redirect with 302 Found and a Location header, and an unknown code with 404 Not Found.
- Write the links table with code as the primary key, and say why the key is the code and not the long URL.
- Say what 301 changes: browsers remember the redirect, which cuts load but hides the clicks.