简体   繁体   中英

Forwarding HTTP requests in Play for Scala

This is the scenario I'm trying to achieve in Play 2.5.x for Scala (all the requests and responses are Json):

  1. Browser sends HTTP Request to URL1.
  2. URL1 enriches the Json it receives with some data, and forwards the entire request to URL2.
  3. URL2 responds to browser.

In the last point, I'm not sure if URL2 can send it back to the browser or has to do it through URL1 (I believe it's the latter).

This is the request in URL1 (URL2 is a simple request/response):

  val request: WSRequest = ws.url("/url2")
  val request2: WSRequest = request.withHeaders("Accept" -> "application/json")
  val data = Json.obj(
        "aaa" -> some_data1,
        "bbb" -> some_data2
   )
   val futureResponse: Future[JsValue] = request2.post(data).map {
            response => response.json
   }

When I send the future I get this exception:

Execution exception[[NullPointerException: scheme]]

How to fix this?

The clue is in the function name - it's ws.url , not ws.uri . You need to specify a complete path. You can use ws.url("http://localhost:9000/url2") , with customised elements if necessary such as the scheme and the port based on your configuration.

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