Json作为可写Scala Play框架

时间:2014-06-17 22:34:41

标签: scala playframework

我正在尝试发送http请求并将其发送给其他服务。我想使用从第一个POST发送的json并将其发送到下一个服务。我遇到的问题是将POST数据转换为json并将其放入新的POST,但它不是Play的类型Writeable

以下是代码:

def postProxyParse(proxyUrl: String) = Action.async { request =>
    var url = buildUrl(request.uri)
    val data = request.body.asJson
    if(url ==""){
      badRequest(null, "Url Not matching proxy possibilities")
    }
    WS.url(url).post(data).map { response =>
       Ok(response.body)
    }
}

我得到的错误是Cannot write an instance of Option[play.api.libs.json.JsValue] to HTTP response. Try to define a Writeable[Option[play.api.libs.json.JsValue]]

1 个答案:

答案 0 :(得分:1)

嗨,所以重点是创建一个代理服务来重定向具有特定网址的帖子请求。这个问题的答案是:

  def postProxyParse(proxyUrl: String) = Action.async { request =>
val url = buildUrl(request.uri)
var data = Json.parse(request.body.asText.get);
if(url ==""){
  badRequest(null, "Url Not matching proxy possibilities")
}
WS.url(url).withHeaders(  "Accept" -> "application/json",
  "Cookie" -> ("sessionId=" + request.cookies.apply("sessionId").value)).post(data).map { response =>
  Ok(data)
}
}

scala代理服务的完整代码是here