简体   繁体   中英

Play Framework Scala Proxy For Http Post

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.

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

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