【问题标题】:How to cUrl a DELETE request to Yesod?如何将 DELETE 请求卷曲到 Yesod?
【发布时间】: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 之类的东西?
  • 也许你在语义上是对的,但路线确实存在。
  • 您很可能需要为任何人提供源代码才能找到问题的根源。

标签: haskell yesod


【解决方案1】:

修好了!这是堆栈中的某种缓存问题,它缓存了旧版本的路由文件。杀死 yesod devel 并运行 stack build --force-dirty 修复它。

【讨论】:

    猜你喜欢
    • 2021-05-31
    • 1970-01-01
    • 2011-12-23
    • 2018-07-08
    • 2018-09-20
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多