HTTP requests and responses
BeginnerWhat a request and a response are made of: methods such as GET, POST and DELETE, status codes such as 201, 404 and 204, headers, and a JSON body.Builds on What happens when you type a URL
POST /api/notes HTTP/1.1Host: example.comContent-Type: application/jsonContent-Length: 25 {"text":"Buy fresh mint"} HTTP/1.1 201 CreatedLocation: /api/notes/42Content-Type: application/jsonContent-Length: 33 {"id":42,"text":"Buy fresh mint"} GET /api/notes/7 HTTP/1.1Host: example.com HTTP/1.1 404 Not FoundEmpty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
4 min read
On this page
What is an HTTP request and response?
HTTP is the language browsers and servers speak. Every exchange is one request from the client and one response from the server, and both have the same shape:
- A first line. For a request, a method and a path (
POST /api/notes). For a response, a status code and its reason (201 Created). - Headers. One
Name: valueper line, describing the message: what the body is, how big it is, who is asking, how long to keep it. - A blank line, then an optional body: HTML, JSON, an image… A
GETrequest has none, and neither does a204 No Contentresponse.
Pages use the same requests as your code does. A link or the address bar sends a GET, a form sends
a GET or a POST, and JavaScript can send any method with .
How do methods, status codes and headers work together?
- The method says what you want.
GETreads,POSTcreates,PUTreplaces,PATCHchanges part of something andDELETEremoves it.GETis safe: it changes nothing.GET,PUTandDELETEare also idempotent: sending them twice has the same effect as once, which is why browsers and tools may retry them, but not aPOST. - The status says how it went, by its first digit:
2xxsuccess:200 OK,201 Created,204 No Content.3xxgo elsewhere:301 Moved Permanently,304 Not Modified.4xxa problem with the request:400 Bad Request,401 Unauthorized(who are you?),403 Forbidden(you can't),404 Not Found.5xxa problem on the server:500 Internal Server Error,503 Service Unavailable.
- Headers carry everything else.
Content-TypeandContent-Lengthdescribe the body,Acceptsays what the client would like back,Locationpoints at a new or moved resource, andCookieandCache-Controlhave lessons of their own.
What do people get wrong about HTTP?
Mini example
Changing part of a note with PATCH. The first request has a mistake, so the server answers with a
4xx and a body that explains it; the second one works:
PATCH /api/notes/42 HTTP/1.1Host: example.comContent-Type: application/json {"text": ""} HTTP/1.1 400 Bad RequestContent-Type: application/json {"error": "text must not be empty"} PATCH /api/notes/42 HTTP/1.1Host: example.comContent-Type: application/json {"text": "Buy fresh mint and lemons"} HTTP/1.1 200 OKQuick check
fetch('/api/notes/7') gets a 404 Not Found. What happens?
Further reading
All steps as text
- Step 1:
A notes app runs in the browser. You type “Buy fresh mint” and press Save, so the page’s JavaScript sends a request to the server with
fetch. - Step 2:
Every request starts with a request line. The method
POSTsays what to do, create something, and the path/api/notessays where.Line 1
- Step 3:
Then the headers, one per line.
Hostnames the site,Content-Typesays the body is JSON, andContent-Lengthgives its size in bytes.Lines 2–4
- Step 4:
A blank line ends the headers, and the body follows: the note itself, as JSON. The request is on its way.
Lines 5–6
- Step 5:
The server reads
Content-Type, parses the JSON and checks it. Broken JSON or an empty text would get a400 Bad Requestinstead. - Step 6:
The note is valid, so the server saves it in the database, which gives it the id 42.
- Step 7:
The response starts with a status line.
201 Createdis a 2xx code, a success, and says more than200 OK: something new now exists.Line 8
- Step 8:
The
Locationheader says where the new note lives:/api/notes/42. That address is how the app will read, change or delete it later.Line 9
- Step 9:
The body is JSON too, as
Content-Typesays, so the page reads it withresponse.json()and shows the note with its id.Lines 10–13
- Step 10:
Later the app asks for note 7 with
GET. A GET only reads: it has no body and changes nothing, so it is safe to repeat.Lines 15–16
- Step 11:
The server looks for note 7 in the database. It was deleted long ago: there is nothing to send.
- Step 12:
404 Not Foundis a 4xx code: the request asked for something that isn’t there.fetchdoesn’t throw on it, so the page must checkresponse.ok.Line 18
- Step 13:
Now you delete note 42. The method
DELETEand the address fromLocationare all the server needs, so the request has no body. - Step 14:
204 No Contentmeans it worked and there is nothing to send back. The page simply removes the note from the list. - Step 15:
Every exchange has the same shape: method, path, headers and maybe a body, answered by a status, headers and maybe a body. The method says what you want; the status says how it went.