Skip to content
YouTurkey Tech
All web topics

HTTP requests and responses

Beginner

What 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 Found
Messages

Empty

0/15Press Play, or step through with the arrows, to begin.
Step 0 of 15

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:

  1. 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).
  2. Headers. One Name: value per line, describing the message: what the body is, how big it is, who is asking, how long to keep it.
  3. A blank line, then an optional body: HTML, JSON, an image… A GET request has none, and neither does a 204 No Content response.

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. GET reads, POST creates, PUT replaces, PATCH changes part of something and DELETE removes it. GET is safe: it changes nothing. GET, PUT and DELETE are also idempotent: sending them twice has the same effect as once, which is why browsers and tools may retry them, but not a POST.
  • The status says how it went, by its first digit:
    • 2xx success: 200 OK, 201 Created, 204 No Content.
    • 3xx go elsewhere: 301 Moved Permanently, 304 Not Modified.
    • 4xx a problem with the request: 400 Bad Request, 401 Unauthorized (who are you?), 403 Forbidden (you can't), 404 Not Found.
    • 5xx a problem on the server: 500 Internal Server Error, 503 Service Unavailable.
  • Headers carry everything else. Content-Type and Content-Length describe the body, Accept says what the client would like back, Location points at a new or moved resource, and Cookie and Cache-Control have 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 OK

Quick check

fetch('/api/notes/7') gets a 404 Not Found. What happens?

Further reading

All steps as text
  1. 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.

  2. Step 2:

    Every request starts with a request line. The method POST says what to do, create something, and the path /api/notes says where.

    Line 1

  3. Step 3:

    Then the headers, one per line. Host names the site, Content-Type says the body is JSON, and Content-Length gives its size in bytes.

    Lines 2–4

  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

  5. Step 5:

    The server reads Content-Type, parses the JSON and checks it. Broken JSON or an empty text would get a 400 Bad Request instead.

  6. Step 6:

    The note is valid, so the server saves it in the database, which gives it the id 42.

  7. Step 7:

    The response starts with a status line. 201 Created is a 2xx code, a success, and says more than 200 OK: something new now exists.

    Line 8

  8. Step 8:

    The Location header says where the new note lives: /api/notes/42. That address is how the app will read, change or delete it later.

    Line 9

  9. Step 9:

    The body is JSON too, as Content-Type says, so the page reads it with response.json() and shows the note with its id.

    Lines 10–13

  10. 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

  11. Step 11:

    The server looks for note 7 in the database. It was deleted long ago: there is nothing to send.

  12. Step 12:

    404 Not Found is a 4xx code: the request asked for something that isn’t there. fetch doesn’t throw on it, so the page must check response.ok.

    Line 18

  13. Step 13:

    Now you delete note 42. The method DELETE and the address from Location are all the server needs, so the request has no body.

  14. Step 14:

    204 No Content means it worked and there is nothing to send back. The page simply removes the note from the list.

  15. 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.