【发布时间】:2018-08-14 15:13:54
【问题描述】:
我在 Yesod 中构建了一个小的 JSON api。为了测试我使用 cUrl 的 API。但是,以下请求返回405 method not allowed:
curl -X DELETE -H "X-User-Name: $name" -H "X-User-Token: $token" -H "accept:application/json" http://localhost:3000/login
路由文件肯定包含DELETE 作为登录路由上的方法。我正在使用defaultYesodMiddleware。
这里是处理程序:
-- ^ Invalidate the user token
deleteLoginR :: Handler Aeson.Value
deleteLoginR = authenticateUser >>= \u ->
let newU = u { DS.token = "" } in
DS.saveEntity newU >>= \case
Left e -> sendResponseStatus status500 (cs e :: Text)
Right _ -> sendResponseStatus status200 ("Logged out" :: Text)
这里是authenticateUser 函数:
{-| Check that a user is properly logged in using the authentication headers -}
authenticateUser :: Handler User
authenticateUser = lookupHeader "X-User-Name" >>= \case
Nothing ->
sendResponseStatus status406 ("X-User-Name not set" :: Text)
Just name ->
lookupHeader "X-User-Token" >>= \case
Nothing ->
sendResponseStatus status406 ("X-User-Token not set" :: Text)
Just token ->
(DS.loadEntities ["User"] constraints Nothing :: Handler (Either Text [User]))
>>= \case
Left e -> sendResponseStatus status500 (cs e :: Text)
Right records -> pure $ List.head records
where constraints = [ Where ("name", Equals, cs name)
, Where ("token", Equals, cs token)
]
和路由声明:
/login LoginR GET POST PATCH DELETE
有人能告诉我吗?
【问题讨论】:
-
向
http://localhost:3000/login发送删除请求似乎不合理。你确定你定义了这条路线吗?可能您打算将删除发送到http://localhost:3000/logout之类的东西? -
也许你在语义上是对的,但路线确实存在。
-
您很可能需要为任何人提供源代码才能找到问题的根源。