HTTP & REST
HTTP is the language of the web and of almost every API you'll automate against. Once you can read a request and a response, debugging a website or a pipeline that calls an API becomes much easier.
Request and response
HTTP is a simple conversation: the client (a browser, curl, a script) sends a request, the server sends back a response. Each has a first line, headers (key: value metadata) and an optional body.
GET /api/users/42 HTTP/1.1 ← method, path, version
Host: api.example.com ← headers
Authorization: Bearer eyJhbGciOi...
Accept: application/json
HTTP/1.1 200 OK ← status code
Content-Type: application/json
Content-Length: 58
{"id": 42, "name": "Demee", "role": "admin"} ← body
HTTP is stateless: the server doesn't remember you between requests. Anything that should carry over (being logged in) has to be sent again every time, usually as a cookie or an Authorization header with a token. That's exactly how this site's login works: after you sign in, the browser sends a session cookie with every request.
Methods
- GET reads data and shouldn't change anything.
- POST creates something or submits data (a form, a new user).
- PUT replaces a resource completely; PATCH changes part of it.
- DELETE removes it.
GET, PUT and DELETE are idempotent: doing them twice has the same effect as doing them once. POST isn't: sending it twice may create two records, which is why browsers warn before resubmitting a form.
Status codes
- 2xx success: 200 OK, 201 Created, 204 No Content.
- 3xx redirect: 301 Moved Permanently, 302 Found (temporary), 304 Not Modified (use your cached copy).
- 4xx the client's fault: 400 Bad Request, 401 Unauthorized (you're not logged in), 403 Forbidden (logged in, but not allowed), 404 Not Found, 429 Too Many Requests (rate limited).
- 5xx the server's fault: 500 Internal Server Error (the app crashed), 502 Bad Gateway (a proxy got no valid answer from the app behind it), 503 Service Unavailable (overloaded or down for maintenance), 504 Gateway Timeout.
HTTPS and TLS
Plain HTTP travels as readable text. HTTPS is HTTP inside a TLS encrypted connection (TLS is the modern version of SSL). During the TLS handshake the server shows a certificate signed by a trusted certificate authority, proving it really is demee.cc; then both sides agree on keys and everything after that is encrypted. HTTPS uses port 443. Free certificates come from Let's Encrypt, or automatically from Cloudflare.
HTTP/2 keeps the same methods and codes but sends many requests at once over a single connection, which makes pages with lots of files load faster.
REST APIs
REST is a common style for web APIs: URLs name resources (nouns), and the HTTP method says what to do with them. The body is usually JSON.
GET /users # list users
POST /users # create a user (details in the JSON body)
GET /users/42 # one user
PUT /users/42 # replace user 42
DELETE /users/42 # delete user 42
You can try any API from the terminal with curl, or with a GUI tool like Postman:
$ curl https://api.github.com/users/octocat # GET
$ curl -I https://demee.cc # headers only
$ curl -v https://demee.cc # show the whole conversation
$ curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"name": "Demee"}'