Get '[JSON] [MyData] app :: Application app = serve-6ren">
gpt4 book ai didi

rest - 如何访问 Servant 中的 http header ?

转载 作者:可可西里 更新时间:2023-11-01 17:00:55 27 4
gpt4 key购买 nike

我有一个带有 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 header 并返回特定响应,例如 http403?

最佳答案

使用 Servant 的 Header ( Servant Docs )

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

然后

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

关于rest - 如何访问 Servant 中的 http header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36307378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com