Kitura TypeSafeHTTPBasic 身份验证在 post body 中寻找凭据?

Kitura TypeSafeHTTPBasic auth looking for credentials in post body?

我在 Kitura 中有一个 TypeSafe Codeable 路由定义如下:

app.router.post("/games") { (auth: BasicAuth, respondWith: @escaping (Game?, RequestError?) -> ()) in 
    ...
}

但是当我发出 get 请求时,我收到 Could not decode received JSON: The required key 'id' not found.。这似乎是路由器试图从 POST body 而不是基本身份验证 header 解析 auth object。如果我将路由更改为 GET,它工作得很好,但我不太了解类型安全可编码路由,而且我对 POST 路由发生的变化感到困惑。如何让我的 BasicAuth 与 POST 和 GET 一样工作?

使用 Kitura 的 Codable 路由时,POST 处理程序期望从消息正文接收 Codable 输入。您可以选择指定一个或多个 TypeSafeMiddleware 要求。

如果你想执行 POST,你会想要匹配 the post() function on Router,它将 Codable 和 TypeSafeMiddleware 作为输入:

app.router.post("/games") { (auth: BasicAuth, input: MyInputType, respondWith: @escaping (Game?, RequestError?) -> ()) in 
    ...
}

你的情况是你实际上匹配 this post() function without TypeSafeMiddleware,其中你的身份验证类型(符合 Codable)被解释为你的输入类型。

如果您不希望 POST 为该请求向服务器发送有效负载,也许您想要的是一个 GET,它匹配 the get() function on Router 只接受 TypeSafeMiddleware 作为输入:

app.router.get("/games") { (auth: BasicAuth, respondWith: @escaping (Game?, RequestError?) -> ()) in 
    ...
}

请注意,TypeSafeMiddleware 是路由处理程序的第一个参数,然后是任何其他输入类型(在 PUT 或 POST 的情况下)。