我有一个小的JSON API,我在Yesod中构建。为了测试API,我使用cUrl。但是,以下请求返回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
有人可以启发我吗?
答案 0 :(得分:0)
解决了!这是堆栈中的某种缓存问题,它已经缓存了路由文件的旧版本。杀死yesod devel
并运行stack build --force-dirty
可以解决此问题。