返回确切的响应/标题? [英] Return exact response/header?

查看:94
本文介绍了返回确切的响应/标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Web应用程序的客户端,我遇到了服务器端路由,该路由只是第三方API的包装.使用分派,我试图使服务器端请求返回 exact 标头和第三方API对客户端AJAX调用的响应.

From the client-side of a webapp, I hit a server-side route which is just a wrapper for a third-party API. Using dispatch, I am trying to make that server-side request return the exact header and response of the third-party API to the client-side AJAX call.

当我这样做时:

val req = host("third-pary.api.com, 80)
val post = req.as("user", "pass") / "route" << Map("key" -> "akey", "val" -> "aval")
Http(post > as.String)

我总是看到对AJAX调用返回的200响应(按预期的那样).我已经看到使用了Either语法,但实际上我更像是Any,因为它只是确切的响应和标头.怎么写?

I always see a 200 response returned to the AJAX call (kind of expectedly). I have seen an Either syntax used, but I'm really more of an Any, as it's just the exact response and header. How would this be written?

我应该提到我在服务器端使用Scalatra,因此本地路由为:

I should mention I'm using Scalatra on the server-side, so the local route is:

post("/route") {

}

这是我正在使用的建议的任何一个匹配示例,但是match语法没有意义-我不在乎是否有错误,我只想返回它.另外,我似乎无法通过此方法返回BODY.

Here is the suggested Either matching example, which I'm playing with, but the match syntax doesn't make sense - I don't care if there is an error, I just want to return it. Also, I can't seem to get the BODY returned with this method.

val asHeaders = as.Response { response =>
  println("BODY: " + response.getResponseBody())
  scala.collection.JavaConverters.mapAsScalaMapConverter(
    response.getHeaders).asScala.toMap.mapValues(_.asScala.toList)
}

val response: Either[Throwable, Map[String, List[String]]] =
  Http(post > asHeaders).either()

response match {
  case Left(wrong) =>
    println("Left: " + wrong.getMessage())
    // return Action with header + body
  case Right(good) =>
    println("Right: " + good)
    // return Action with header + body
}

理想情况下,解决方案返回Scalatra ActionResult(responseStatus(status, reason), body, headers).

Ideally, the solutions returns the Scalatra ActionResult(responseStatus(status, reason), body, headers).

推荐答案

使用Dispatch时,获取响应标头实际上非常容易.例如0.9.4:

It's actually very easy to get response headers while using Dispatch. For example with 0.9.4:

import dispatch._
import scala.collection.JavaConverters._

val headers: java.util.Map[String, java.util.List[String]] = Http(
   url("http://www.google.com")
)().getHeaders

现在,例如:

scala> headers.asScala.mapValues(_.asScala).foreach {
     |   case (k, v) => println(k + ": " + v)
     | }
X-Frame-Options: Buffer(SAMEORIGIN)
Transfer-Encoding: Buffer(chunked)
Date: Buffer(Fri, 30 Nov 2012 20:42:45 GMT)
...

如果经常这样做,最好将其封装起来,例如:

If you do this often it's better to encapsulate it, like this, for example:

val asHeaders = as.Response { response =>
  scala.collection.JavaConverters.mapAsScalaMapConverter(
    response.getHeaders
  ).asScala.toMap.mapValues(_.asScala.toList)
}

现在您可以编写以下内容:

Now you can write the following:

val response: Either[Throwable, Map[String, List[String]]] =
  Http(url("http://www.google.com") OK asHeaders).either()

您将进行错误检查,漂亮的不可变集合等.

And you've got error checking, nice immutable collections, etc.

这篇关于返回确切的响应/标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆