【发布时间】:2016-12-15 00:48:30
【问题描述】:
我正在调用外部 API,如果状态代码不同于 OK,我想将结果“按原样”返回给用户:
val connectionFlow: Flow[HttpRequest, HttpResponse, Future[Http.OutgoingConnection]] =
Http().outgoingConnection("akka.io")
def responseFuture: Future[HttpResponse] =
Source.single(HttpRequest(uri = "/"))
.via(connectionFlow)
.runWith(Sink.head)
val fooRoutes = path("foo"){
get {
complete(
responseFuture.flatMap{ response =>
case OK =>
Unmarshal(response.entity.withContentType(ContentTypes.`application/json`)).to[Foo]
case _ => response //fails
})}}
在状态码不是 OK 的情况下,如何在不执行类似操作的情况下返回“原样”响应:
Unmarshal(response.entity).to[String].flatMap { body =>
Future.failed(new IOException(s"The response status is ${response.status} response body is $body"))}
【问题讨论】:
标签: scala akka-stream akka-http