问题描述
我有一个Akka Streams源,它流经流程并发布HTTP请求:
I have an Akka Streams source that goes through a flow and posts an HTTP request:
source.map(toRequest)
.via(Http().outgoingConnection(host))
.map(toMessage)
假设 toRequest
方法将一个字符串映射到 HttpRequest
和 toMessage
方法将 HttpResponse
映射到处理下游所需的消息类。假设消息类需要包含一些原始信息。
Suppose that the toRequest
method maps a string to an HttpRequest
, and the toMessage
method maps the HttpResponse
to a message class that is needed to process downstream. Suppose that the message class needs to contain some of the original information.
是否可以获得原始的 HttpRequest
来自 HttpResponse
?如果没有,有什么办法可以保留一些原始信息?
Is it possible to get the original HttpRequest
from the HttpResponse
? If not, is there any way to keep some of the original information?
推荐答案
一种方法是使用和自定义案例类,其中包含您要向下游传播的信息。例如:
One approach is to use a Future
-based variant of the client API and a custom case class that holds the information you want to propagate downstream. For example:
case class Message(request: HttpRequest, response: HttpResponse)
source
.map(toRequest)
.mapAsync(parallelism = 3) { request => // adjust the level of parallelism as needed
Http().singleRequest(request).map(response => Message(request, response))
}
// continue processing; at this point you have a Source[Message, _]
这篇关于Akka流/ HTTP:从响应中获取原始请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!