sarah w
sarah w

Reputation: 3515

how to pass parameters in url without showing in url in playframework

In my Controller i have two Actions ActionA and ActionB ,In ActionA i have a string which i want to send to ActionB for that i am doing it like this

 object MyController extends Controller {

    def ActionA= Action {
       var str="abc"
          Redirect(controllers.routes.MyController.ActionB(str))
      }

    def signupProcessing1(token:String)= Action {
        Ok("string is " + token)   
      } 
    }

in the route file

GET    /user/actionB controllers.MyController.ActionB(token:String)  

GET   /user/actionA  controllers.MyUserController.ActionA

when i hit localhost:9000/user/actionA in browser its redirected to this url

localhost:9000/user/actionB?token="abc"

i do not want to show this string "abc" in the url for that i used POST instead of GET

POST    /user/actionB controllers.MyController.ActionB(token:String)  

GET   /user/actionA  controllers.MyController.ActionA

But it gives Exception

`Action not found` 

    For request 'GET /user/actionB?token=abc'

Please help me how can i pass parameters from one Action to another without showing in the url

Upvotes: 1

Views: 1385

Answers (1)

Jonas Anso
Jonas Anso

Reputation: 2067

The HTTP specification for redirect is not meant to change the method from GET to POST.

But there are a few things you can do to solve your problem.

The simplest approach is to not Redirect, just forward to your action:

  def ActionA = {
    val str = "abc"
    ActionB(str)
  }

  def ActionB(token: String) = Action {
    Ok("string is " + token)
  }

In this case the URL will remain ActionA

Another approach is to use a cookie

  def ActionA = Action {
    val str="abc"
    Redirect(controllers.routes.MyController.ActionB).withCookies(
      Cookie("token", str))
  }

  def ActionB = Action { r =>
    r.cookies.get("token") match {
      case Some(cookie) =>
        val token = cookie.value
        Ok("string is " + token)
      case _ => Unauthorized
    }
  }

In this case the url will change to ActionB

If you are interested in Authentication and Authorization implementations you can take a look at some of them here

Upvotes: 3

Related Questions