Course outline · 0% complete

0/25 lessons0%

Course overview →

Methods and status codes

lesson 4-3 · ~10 min · 12/25

Methods: the verb of the request

The method at the start of the request line says what kind of action you want:

MethodMeaningHas a body?
GETread a resourceno
POSTcreate something / submit datayes
PUTreplace a resourceyes
PATCHpartially update a resourceyes
DELETEremove a resourceusually no

Browsers mostly send GET (every page, image, and script) and POST (form submissions, logins). The others show up constantly in APIs. A useful rule: GET must be safe, meaning it only reads and changes nothing, which is why a GET can be cached, retried, and prefetched freely (unit 8 builds on this).

REST: the convention that makes methods useful

Methods only pay off if everyone agrees what they act on. REST (Representational State Transfer) is the dominant convention for that: every thing the server manages — a note, a user, an order — is a resource with its own path, and the HTTP method is the verb applied to it:

RequestMeaning
GET /noteslist the notes
GET /notes/17read note 17
POST /notescreate a new note
PATCH /notes/17update part of note 17
DELETE /notes/17remove note 17

An API built this way is called a REST API. The convention exists because the alternative was chaos: before it, every service invented its own verbs (/getNote?op=del&id=17), and every integration meant reading someone's manual. With REST, an engineer who knows the resource path can usually guess the entire API. You will drive one with curl in unit 5.

Quiz

Following REST conventions, what does the request PATCH /users/42 do?

Status codes: the result, in one number

The first digit of the status code puts it in a class:

ClassMeaningFamous members
2xxsuccess200 OK, 201 Created
3xxredirect, look elsewhere301 Moved Permanently, 304 Not Modified
4xxclient's fault400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found
5xxserver's fault500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable

The 4xx/5xx split is the single most useful debugging fact in HTTP: a 4xx means your request was wrong (bad URL, missing login, no permission), a 5xx means the server broke while handling a request it understood. You will lean on this hard in the unit 9 capstone.

Code exercise · bash

Your turn. Classify each status code by its first digit using a case statement. Patterns like 2*) match any code starting with 2. Print each code and its class as shown in the expected output.

Quiz

An API returns 403 Forbidden when your script calls it. Whose side is the problem on?

Problem

You want your script to remove the resource at /api/notes/17 on a REST API. Which HTTP method belongs in the request line? (One word.)