1

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]]

5
  • 2
    Can you give us the exact error you get? Commented Jun 17, 2014 at 23:54
  • Your code as written is not producing that error. Are you sure you didn't write Ok(data) or Ok(response.body.asJson)? Commented Jun 18, 2014 at 13:31
  • @wingedsubmariner You're right my bad If you look now the error is being thrown at the post when I am trying to pass the data to the new service Commented Jun 18, 2014 at 13:38
  • Try val data = request.body.asJson.getOrElse(badRequest(null, "Bad JSON")). Commented Jun 18, 2014 at 14:11
  • @wingedsubmariner I added what ended up working... not sure if you would be interested Commented Jun 19, 2014 at 15:50

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.