缺少 header 的 Akka HTTP 拒绝
Akka HTTP reject with missing header
我有以下方法:
private def initiateAuth(req: LoginRequest, userAgent: String, deviceId: String) = {
extractClientIP { clientIp =>
extractRequestContext { implicit ctx =>
if (clientIp.toOption.isEmpty) reject(Rejections.validationRejection("abc"))
val xForwardedFor = clientIp.toOption.map(_.getHostAddress).get
mapToResponse(userAuthenticator.auth(req.username, req.password, deviceId, xForwardedFor, userAgent))
}
}
}
真正奇怪的是 Rejections.validationRejection("abc")
有效,但是当我使用 Rejections.missingHeader("abc")
时出现错误:
两者有什么区别?我很困惑,因为 MissingHeaderRejection 实现了拒绝,那么为什么类型不匹配?
在您的 if 语句中,当 clientIp 选项为空时,if 的结果将被忽略。我最好使用模式匹配或 if - else 表达式
我有以下方法:
private def initiateAuth(req: LoginRequest, userAgent: String, deviceId: String) = {
extractClientIP { clientIp =>
extractRequestContext { implicit ctx =>
if (clientIp.toOption.isEmpty) reject(Rejections.validationRejection("abc"))
val xForwardedFor = clientIp.toOption.map(_.getHostAddress).get
mapToResponse(userAuthenticator.auth(req.username, req.password, deviceId, xForwardedFor, userAgent))
}
}
}
真正奇怪的是 Rejections.validationRejection("abc")
有效,但是当我使用 Rejections.missingHeader("abc")
时出现错误:
两者有什么区别?我很困惑,因为 MissingHeaderRejection 实现了拒绝,那么为什么类型不匹配?
在您的 if 语句中,当 clientIp 选项为空时,if 的结果将被忽略。我最好使用模式匹配或 if - else 表达式