简体   繁体   中英

Json As Writeable Scala Play Framework

I am trying to take an http request and send it to another service. I want to use the json sent from the first POST and send it on to the next service. The problem I am having is turning the POST data as json and put it into a new POST but it is not Play's type Writeable .

Here is the code:

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)
    }
}

The Error I am getting is 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]] 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]]

Hi so the point of this was to create a proxy service to redirect post requests with a specific url. The answer to this problem is:

  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)
}
}

The full code for a scala proxy service is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM