HTTP and REST
The protocol every API speaks, learned by answering it: what a request and a response carry, resources and the methods that act on them, status codes that say what happened, representations the client chooses, and caching that saves the round trip. Written on Hono, the smallest framework on this site, so the lessons are about HTTP rather than a framework.
- lessons
- 5
- level
- beginner
- time
- 2 hours
Messages
What a request carries to the server and what the response carries back.
- 1Requests and responses
What travels between a client and your server: a method, a path, a query string, headers and a body one way, and a status, headers and a body the other. Build a server that describes every request it receives.
Read the theory
Resources
Things with names, the methods that act on them, and the status codes that say how it went.
- 2Resources and methods
A URL names a thing and a method says what to do to it. Build the full set of cat routes, and see why PUT and DELETE can be repeated safely while POST cannot.
Read the theory - 3Status codes
A status code is the first thing a client reads and often the only thing it acts on. Replace a crash with a 400, tell a bad request from a conflict, refuse a method with the methods that work, and move a path without breaking its clients.
Read the theory
Representations
The forms a resource takes on the wire, and how clients and caches avoid fetching it twice.
- 4Content negotiation
One resource, several representations. Answer the list as JSON or CSV depending on what the client's Accept header prefers, refuse what you cannot produce or read with 406 and 415, and tell caches the answer depends on the header.
Read the theory - 5Caching and conditional requests
The fastest request is the one never sent, and the next fastest is answered with "you already have it". Let caches keep what never changes, tag each cat with an ETag, answer 304 to a client whose copy is current, and refuse a write based on a copy that is not.
Read the theory