Routing
noun
say it: “ROW-ting” /ˈraʊtɪŋ/
In a web API, choosing which piece of code answers each request, from its method, such as GET, and its path, such as /plots/7.
What is routing in a web API?
Routing is choosing which code answers a request, from its method (GET, POST, ...) and its path. By hand it's an
if for each route; a framework such as FastAPI does it for you from decorators like @app.get("/plots/{plot_id}").
def handle(method, path):
if method == "GET" and path == "/plots":
return 200, [{"id": 7, "crop": "tomatoes"}]
if method == "GET" and path == "/plots/7":
return 200, {"id": 7, "crop": "tomatoes"}
return 404, {"detail": "Not Found"}
print(handle("GET", "/plots/7")) # (200, {'id': 7, 'crop': 'tomatoes'})
print(handle("GET", "/weeds")) # (404, {'detail': 'Not Found'})
In a sentence
- “Good routing sends GET /plots/7 to the function that looks up plot 7, and DELETE /plots/7 to the one that removes it.”
- “Before FastAPI, Jordan wrote the garden API's routing by hand, one if statement for each method and path.”
Level: Apprentice · More REST API terms at this level
Updated