我有一个简单的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,具体取决于条件?
答案 0 :(得分:3)
使用Header
(servant)
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" }