简体   繁体   中英

“Redirect” to an external URL using play framework 2.7

I am trying to redirect to an external URL with some authorization code attached to it. I am using the "Redirect" function available in play framework using Scala. But instead of redirecting to the provided URL, the message in "Ok" gets printed and is not redirected

I am a beginner in Play and Scala. It is my understanding that an "Action" should send a "Result". This "Result" can either be "Ok" or a "Redirect". In the below code: 1) If I don't mention "Ok", there is a compile error 2) If I execute the code below, it doesn't get redirected, it just prints the message in "Ok"

// This is inside a controller
// it is defined as a GET in the routes file
def test = Action { implicit request =>
  async {
    await(userAuth.getAuth()) match{
      case Some(userAuth) if <>
        val url = <FunctionReturnsUrlwithcode>
        Redirect(url)
      case _ if flag
        val url = <FunctionReturnsUrlwithcode>
        Redirect(url)
    }
    Ok("Some message") // Without this , there is an error
}

I assume this is pseudocode, since that pattern matching isn't syntactically correct...

Your pattern matching isn't exhaustive. For example:

def test(): String = {
  val x = Some(1)

  x match {
    case Some(y) if y == 2 => return "matched 1"
    case _ if false == true => return "matched 2"
  }

  "matched nothing"
}

The above code will return "matched nothing" , since 1 != 2 and false != true . There needs to be a default case, otherwise if any of the specified conditions aren't met it'll ignore the pattern matching altogether.

In your case, I'm guessing that neither of the conditions in your pattern matching are being met. Therefore, the pattern matching is skipped over and Ok(whatever) is returned - if you take out the Ok , your app will blow up since an Action must return a Result . Your code will also return the Ok if you write it like this:

def test = Action { implicit request =>
  async {
    await(userAuth.getAuth()) match{
      case Some(userAuth) if <> =>
        val url = <FunctionReturnsUrlwithcode>
        Redirect(url)
      case _ if flag =>
        val url = <FunctionReturnsUrlwithcode>
        Redirect(url)
      case _ =>
        Ok("Some message")
    }
  }
}

Also,

This "Result" can either be "Ok" or a "Redirect"

This is not true; it isn't limited to only two values. It can be a number of other things, such as NoContent , Created , BadRequest , InternalServerError , etc. (basically any valid HTTP status).

You can try the below:

def test: Action[AnyContent] = Action.async { implicit request =>
//your logic
async {
await(userAuth.getAuth()) match{
  case Some(userAuth) if <> =>
    val url = <FunctionReturnsUrlwithcode>
    (Redirect(url, Map("traceid"-> Set("trace")))
  case _ if flag =>
    val url = <FunctionReturnsUrlwithcode>
    (Redirect(url, Map("traceid"-> Set("trace")))
  case _ =>
    Ok("Some message")
}
}

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