liveBook · Manning

Routing

As explained in Learn Haskell by Example

A route is a URL path pattern that identifies an API operation and may contain variable segments.

Routing maps URL paths and HTTP requests to API operations. In Servant, a route is encoded as a type, using path segments, request descriptions, response types, and combinators. section 17.1.1

Build a route from these pieces:

  • Use string type-level segments such as "entries" or "add" to describe the URL path. section 17.1.1
  • Use :> to attach request and response information, such as Get '[JSON] [TodoEntry] or ReqBody '[JSON] TodoEntry. section 17.1.1
  • Use :<|> to combine several endpoint descriptions into one API. section 17.1.1
  • Use Capture to keep a variable path segment, such as an integer todo identifier. section 17.1.1

Todo-list routes

A typed API can expose GET /entries, POST /add with a JSON TodoEntry body, and PATCH /toggle/:id with an integer capture. Servant can reject request bodies that do not match the declared specification. section 17.1.1

How Learn Haskell by Example covers it

How deep it goes
Encodes multiple typed endpoints and variable segments, but defers detailed explanation of DataKinds and TypeOperators.
Examples
Uses simple GET endpoints, JSON responses, POST request bodies, captured identifiers, and a typed todo-list API.
What it assumes
Uses Haskell type-level syntax and requires the DataKinds and TypeOperators language extensions.
What you can do after
Apply routing via Servant by declaring typed paths, composing endpoints, capturing variables, and specifying request and response formats.
How it connects
Connects routes with APIs, endpoints, HTTP verbs, JSON, request bodies, response types, and type operators.

Also explained in

Compare how 22 books explain routing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.