2

我有一个带有 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?

4

1 回答 1

4

使用Header仆人

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" }
于 2016-03-30T11:34:10.733 回答