如何在 Servant 中访问 http headers?
How can I get access to http headers in Servant?
我有一个带 rest 的简单 servant 应用程序 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 headers 和 return 特定响应,例如 http403?
使用 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" }
我有一个带 rest 的简单 servant 应用程序 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 headers 和 return 特定响应,例如 http403?
使用 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" }