如何实现Akka HTTP Request Post Client Headers Authorization
How to implement Akka HTTP Request Post Client Headers Authorization
我正在尝试为 post 请求实施 Akka http 客户端,其中授权将在 header 中由 post 人提供。我无法授权,以下是我的代码
def main(args: Array[String]) {
implicit val system: ActorSystem = ActorSystem()
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val executionContext: ExecutionContextExecutor = system.dispatcher
val requestHeader = scala.collection.immutable.Seq(RawHeader("Authorization", "admin"))
val requestHandler: HttpRequest => HttpResponse = {
case HttpRequest(POST, Uri.Path("/GetTrackerData"), requestHeader, entity, _) =>
val chunk = Unmarshal(entity).to[DeviceLocationData]
val deviceLocationData = Await.result(chunk, 1.second)
val responseArray = "Authorized"
HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, responseArray)
)
case r: HttpRequest =>
println(r.uri.toString())
r.discardEntityBytes() // important to drain incoming HTTP Entity stream
HttpResponse(404, entity = "Unknown resource!")
}
val bindingFuture = Http().bindAndHandleSync(requestHandler, "0.0.0.0", 7070)
println(s"iot engine api live at 0.0.0.0:7070")
sys.addShutdownHook({
println("ShutdownHook called")
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
})
}
无论我从post男人那里得到什么价值。它服务于请求。我在跳过什么?
我的用例是显示的结果只有在授权后才会显示
这是一个问题:
case HttpRequest(POST, Uri.Path("/GetTrackerData"), requestHeader, entity, _) =>
这与 requestHeader
的当前值不匹配,它创建了一个包含 HttpRequest
当前 header 的新值 requestHeader
。所以这个匹配不会检查 header 的 Authorization
字段。您将必须手动执行此操作。
更一般地说,Akka HTTP 中的 Route
支持是一种更清洁、更强大的服务器实现方式。
您正在 HttpRequest
上进行模式匹配。
您在那里使用的 requestHeader
不是您之前指定的那个,而是来自 HttpRequest 本身的 headers。
解决它的一种方法是检查 headers:
中的值
case HttpRequest(HttpMethods.POST, Uri.Path("/GetTrackerData"), headers, entity, _)
if (headers.exists(h => h.name == "Authorization" && h.value == "admin")) =>
我正在尝试为 post 请求实施 Akka http 客户端,其中授权将在 header 中由 post 人提供。我无法授权,以下是我的代码
def main(args: Array[String]) {
implicit val system: ActorSystem = ActorSystem()
implicit val materializer: ActorMaterializer = ActorMaterializer()
implicit val executionContext: ExecutionContextExecutor = system.dispatcher
val requestHeader = scala.collection.immutable.Seq(RawHeader("Authorization", "admin"))
val requestHandler: HttpRequest => HttpResponse = {
case HttpRequest(POST, Uri.Path("/GetTrackerData"), requestHeader, entity, _) =>
val chunk = Unmarshal(entity).to[DeviceLocationData]
val deviceLocationData = Await.result(chunk, 1.second)
val responseArray = "Authorized"
HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, responseArray)
)
case r: HttpRequest =>
println(r.uri.toString())
r.discardEntityBytes() // important to drain incoming HTTP Entity stream
HttpResponse(404, entity = "Unknown resource!")
}
val bindingFuture = Http().bindAndHandleSync(requestHandler, "0.0.0.0", 7070)
println(s"iot engine api live at 0.0.0.0:7070")
sys.addShutdownHook({
println("ShutdownHook called")
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
})
}
无论我从post男人那里得到什么价值。它服务于请求。我在跳过什么?
我的用例是显示的结果只有在授权后才会显示
这是一个问题:
case HttpRequest(POST, Uri.Path("/GetTrackerData"), requestHeader, entity, _) =>
这与 requestHeader
的当前值不匹配,它创建了一个包含 HttpRequest
当前 header 的新值 requestHeader
。所以这个匹配不会检查 header 的 Authorization
字段。您将必须手动执行此操作。
更一般地说,Akka HTTP 中的 Route
支持是一种更清洁、更强大的服务器实现方式。
您正在 HttpRequest
上进行模式匹配。
您在那里使用的 requestHeader
不是您之前指定的那个,而是来自 HttpRequest 本身的 headers。
解决它的一种方法是检查 headers:
中的值case HttpRequest(HttpMethods.POST, Uri.Path("/GetTrackerData"), headers, entity, _)
if (headers.exists(h => h.name == "Authorization" && h.value == "admin")) =>