A request and its response
After this: Read a request handler: method and path in, status code and body out, and say what 200, 301, 404 and 500 mean.
On this page · 6 sections · 3 checks
The browser now has the shop's address and an open connection. It sends its request, and the server sends a response. What do those two messages say? And what does the code on the server that answers them look like?
What a request says
Browsers and servers write their messages in a shared format called . A request starts with two things:
| Part | Example | What it says |
|---|---|---|
GET | what kind of request it is: GET means "send me this" | |
/products/42 | which thing on the server the request is about |
So the request for the desk lamp page says, in short: GET /products/42. Another common method is POST, which sends data to the server, for example a filled-in order form.
What a response says
The response also starts with two things: a , a number that says how the request went, and a , the content itself. Four status codes are worth knowing by heart:
| Code | Meaning | Example |
|---|---|---|
| 200 | It worked; the body is what you asked for. | the desk lamp page |
| 301 | It has moved for good; the response names the new path. | an old link to a renamed page |
| 404 | There is no such thing. | /products/999, a product the shop does not have |
| 500 | The server failed while answering. | a bug in the server's code |
The first digit tells the family: 2 means it worked, 3 means look somewhere else, 4 means a problem with the request, and 5 means a problem on the server.
The server's code, by hand
On the server, the code that answers a request can be a plain function. It takes the method and the path, and gives back a status code and a body. Call it handle. The shop keeps two products: 42 is a desk lamp and 7 is a blue mug.
Work through handle("GET", "/products/42"):
- Is the method GET, and does the path start with
/products/? Yes to both. - Take what comes after
/products/: the product id42. - Is
42one of the shop's products? Yes, so the answer is status 200 with the bodyDesk lamp.
Now handle("GET", "/products/999"). Steps 1 and 2 go the same way and give the id 999. At step 3, 999 is not a product, so the function reaches its last line and answers 404 with the body Not found.
The code
products = {"42": "Desk lamp", "7": "Blue mug"}
def handle(method, path): if method == "GET" and path.startswith("/products/"): product_id = path[len("/products/"):] if product_id in products: return 200, products[product_id] return 404, "Not found"
status, body = handle("GET", "/products/42")print(status, body)status, body = handle("GET", "/products/999")print(status, body)200 Desk lamp 404 Not found
import java.util.Map;
class Response { int status; String body;
Response(int status, String body) { this.status = status; this.body = body; }}
public class Main { static Map<String, String> products = Map.of("42", "Desk lamp", "7", "Blue mug");
static Response handle(String method, String path) { if (method.equals("GET") && path.startsWith("/products/")) { String productId = path.substring("/products/".length()); if (products.containsKey(productId)) { return new Response(200, products.get(productId)); } } return new Response(404, "Not found"); }
public static void main(String[] args) { Response r1 = handle("GET", "/products/42"); System.out.println(r1.status + " " + r1.body); Response r2 = handle("GET", "/products/999"); System.out.println(r2.status + " " + r2.body); }}200 Desk lamp 404 Not found
Here is what each part does:
productsis a dictionary (a map in Java) from product id to name. It stands in for the database the server would ask in a real shop.- The first
ifchecks the method and the start of the path. path[len("/products/"):]path.substring("/products/".length()) cuts off the first 10 characters,/products/, and keeps the rest: the product id. - The second
iflooks the id up. When it is there, the function returns 200 and the product's name. - Anything else falls through to the last line: 404 and
Not found.
Run it
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, read the Java version above, and paste it into a Java tool on your computer when you want to run it.
Change 999 to 7 and run it again: the second line becomes 200 Blue mug.
Check yourself
The same handler gets two requests. Read the second path carefully. What does the program print?
products = {"42": "Desk lamp", "7": "Blue mug"}
def handle(method, path): if method == "GET" and path.startswith("/products/"): product_id = path[len("/products/"):] if product_id in products: return 200, products[product_id] return 404, "Not found"
status, body = handle("GET", "/products/7")print(status, body)status, body = handle("GET", "/product/42")print(status, body)import java.util.Map;
class Response { int status; String body;
Response(int status, String body) { this.status = status; this.body = body; }}
public class Main { static Map<String, String> products = Map.of("42", "Desk lamp", "7", "Blue mug");
static Response handle(String method, String path) { if (method.equals("GET") && path.startsWith("/products/")) { String productId = path.substring("/products/".length()); if (products.containsKey(productId)) { return new Response(200, products.get(productId)); } } return new Response(404, "Not found"); }
public static void main(String[] args) { Response r1 = handle("GET", "/products/7"); System.out.println(r1.status + " " + r1.body); Response r2 = handle("GET", "/product/42"); System.out.println(r2.status + " " + r2.body); }}The handler's last line is missing. Complete it so that a product the shop does not have gets the status code for "not found" and the body Not found, and the program prints 404 Not found.
products = {"42": "Desk lamp", "7": "Blue mug"}
def handle(method, path): if method == "GET" and path.startswith("/products/"): product_id = path[len("/products/"):] if product_id in products: return 200, products[product_id] ____________________ # ← the answer when there is no such product
status, body = handle("GET", "/products/999")print(status, body)import java.util.Map;
class Response { int status; String body;
Response(int status, String body) { this.status = status; this.body = body; }}
public class Main { static Map<String, String> products = Map.of("42", "Desk lamp", "7", "Blue mug");
static Response handle(String method, String path) { if (method.equals("GET") && path.startsWith("/products/")) { String productId = path.substring("/products/".length()); if (products.containsKey(productId)) { return new Response(200, products.get(productId)); } } ____________________ // ← the answer when there is no such product }
public static void main(String[] args) { Response r = handle("GET", "/products/999"); System.out.println(r.status + " " + r.body); }}A request for /products/42 reaches the server, but the server's code has a bug and stops with an error while it builds the page. Which status code should the response carry?
- Name the parts of a request (a method such as GET and a path such as /products/42) and of a response (a status code and a body).
- Say what the common status codes mean: 200 found, 301 moved, 404 not found, 500 the server failed.
- Read a request handler as a plain function: method and path in, status code and body out.
- Complete the handler's last line, which answers 404 for anything it does not have.