简体   繁体   English

适用于Http的Play Framework Scala代理

[英]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. scala的框架,并尝试为http请求,GET和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. GET操作似乎在起作用,问题出在POST操作上,我无法传递请求的有效负载。

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) 我必须将parse.json添加到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")
    )
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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