An HTTP method is a standardized token that states the intended operation for a request. In Servant, methods become part of an API type: GET can retrieve JSON or return no content, while POST can accept a JSON request body. PATCH and other methods are available for additional endpoint operations. section 17.1.1
Servant combines methods with paths, request data, and response types:
- GET describes endpoints such as /simple/example and can specify JSON response values. section 17.1.1
- POST can require a JSON body through ReqBody; Servant rejects requests whose bodies do not match the specification. section 17.1.1
- PATCH can update a resource identified with Capture, while DELETE and other HTTP methods can also be declared. 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 shows GET for listing entries, POST for adding one, and PATCH for toggling an identified entry. section 17.1.1









