An API endpoint is a network-accessible operation described by a route, an HTTP method, and request or response data. In Servant, endpoints are declared together as one API type. section 17.1.1
Build endpoint definitions by combining:
- Use (:>) to combine path information with a request description, such as a GET operation. section 17.1.1
- Use (:<|>) to combine multiple endpoint descriptions into one API. section 17.1.1
- Specify response formats such as JSON and result types such as SomeType or [TodoEntry]. section 17.1.1
- Require request bodies with ReqBody; Servant rejects bodies that do not match the declared specification. section 17.1.1
- Add dynamic route values with Capture, supporting endpoints such as /select/{id} or /toggle/{id}. section 17.1.1
Listing 17.1 A type for the todo list API server
{-# LANGUAGE TypeOperators #-} #1
{-# LANGUAGE DataKinds #-} #1
module Todos.Api where
import Servant.API
import Todos.Types
type TodoApi =
"entries" :> Get '[JSON] [TodoEntry] #2
:<|> "add" :> ReqBody '[JSON] TodoEntry :>
PostCreated '[JSON] Int #3
:<|> "toggle" :> Capture "id" Int :> PatchNoContent #4
A complete todo API combines listing, creation, and update endpoints, including JSON bodies, response types, and a captured integer identifier. section 17.1.1

























