Play Framework Scala Proxy For Http Post

2019-04-01 20:04发布

问题:

I'm using play! framework with scala and trying to create a proxy for http requests, GET and POST.

The GET actions seems to be working, the issue is with the POST action, where I'm not able to pass the payload of the request.

I tried several things, like the code below, but none seems to be working.

  def postAction(query: String) = Action.async { implicit request =>
    val data = if (request.body.asText != None) request.body.asText.get else ""
    WS.url(DEMO_URL + query).post(data).map(resp => Ok(resp.body).as("application/json"))
  }

Last thing to mention is that I'm new to both play! and scala.

回答1:

I had to add parse.json the Action.async(parse.json)

The code now is much simpler and looks like this:

  def postAction(query: String) = Action.async(parse.json) { implicit request =>
    WS.url(DEMO_URL + query).post(request.body).map(resp =>
      Ok(resp.body).as("application/json")
    )
  }