如何在Servant中访问http标头?

时间:2016-03-30 11:24:38

标签: rest http haskell http-headers servant

我有一个简单的servant应用程序,其中包含rest api:

type API = "items" :> Get '[JSON] [MyData]

app :: Application
app = serve api server

api :: Proxy API
api = Proxy

server :: Server API
server = getItems

getItems :: ExceptT ServantErr IO [MyData]
getItems = ......

startApp :: IO ()
startApp = run 1234 app

如何访问http标头并返回某个响应,例如http403,具体取决于条件?

1 个答案:

答案 0 :(得分:3)

使用Headerservant

type API = Header "Auth-token" String :> "items" :> Get '[JSON] [MyData]

然后

handler :: Maybe String -> ExceptT ServantErr IO [MyData]
handler (Just "secret-code") = right [mydata]
handler _                    = left $ err403 { errBody = "no access" }