Is there a quick way to redirect a request to a scala Play structure

I'm looking for something like

def proxy = Action.async { implicit req =>
  //do something with req
  val newRequest = req.map( r = r.path = "http://newurl");
  forward(newRequest)
}

I saw that there is a method redirect, but this allows me to pass request parameters, and not everything else, headers, etc.

I hope that there is something built, so I do not need to create it myself.

+4
source share
1 answer

I'm not sure if this meets your requirements, but you have a peek at Play WS .

forwardTo URL-, . Spring, .

/**
 * Like an internal redirect or an proxy. The URL in the browser doesn't
 * change.
 */
public Promise<Result> forwardTo(String url) {
    Promise<WS.Response> response = WS.url(url).get();
    return response.map(new Function<WS.Response, Result>() {
        public Result apply(WS.Response response) {
            // Prevent browser from caching pages - this would be an
            // security issue
            response().setHeader("Cache-control", "no-cache, no-store");
            return ok(response.getBody()).as("text/html");
        }
    });
}

( Play 2.2.3)

0

Source: https://habr.com/ru/post/1584320/


All Articles