Using POST routes parameters in Play Framework

Mattias

I have a login route that should transmit its input parameters as POST variables. I have defined my route like this:

POST    /v1/accounts/login          controllers.v1.Accounts.login(username: String, password: String)

and my Controller is like this:

object Accounts extends Controller {
  def login(username: String, password: String) = Action {
    Ok("Foo " + username)
  }
}

When I test this route using Chromes Advance REST Client it only works for GET parameters and not if I send it as application/x-www-form-urlencoded POST form data.

The Play Framework documentation never actually mentions POST parameters but neither does it say that it does not work.

I think it might get it to work if I create a Form and bind my request to that but that seems needlessly complex.

Is there really no way to bind POST parameters using the routes file?

biesior

Route = resolving params inside the URL = sending params via GET.

That means, that you are trying to send POST request by... GET params... where's the sense ?

James Roper explains that:

At routing time, Play hasn't yet consumed the request body, and so hasn't parsed the submitted form. - and you don't want it to either, because it's your action that decides how/whether the request body gets parsed, streamed, sent elsewhere, if Play did this at routing time, it would limit what you could do in an action.

From security point of view it's definitely bad idea to leave credentials in logs of every machine in the path of client.

Instead you should do it with common form handling way like described in base form documentation:

route:

POST    /v1/accounts/login      controllers.v1.Accounts.login

action:

val userForm = Form(
  tuple(
    "username" -> text,
    "password" -> text
  )
)

def login = Action { implicit request =>
  val (username, password) = userForm.bindFromRequest.get
  Ok("Hello " + username + ", you're trying to login with: " + password)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using POST routes parameters in Play Framework

From Dev

Play Framework: POST request with parameters

From Dev

how to define optional path parameters in Play Framework routes

From Dev

Play framework routes

From Dev

play framework - bind enum in routes

From Dev

Play Framework, REST, Routes, and Controllers

From Dev

Scala backticks in Play Framework Routes

From Dev

Domain routes in Play Framework 2.3

From Dev

Play Framework Error in Routes file

From Dev

Using parameters in Laravel routes

From Dev

play framework- how to send post request using java code in play framework

From Dev

play framework- how to send post request using java code in play framework

From Dev

Send POST request with a body using scala and play framework

From Dev

Non-capturing wildcards in Play Framework routes

From Dev

Iterating all Play Framework routes in Test

From Dev

split routes in multiple files for play framework 2.2

From Dev

Long dynamic routes in play framework 2

From Dev

Play Framework @routes.Assets.at Compilation Error

From Dev

Play framework - call controller in routes with Class parameter

From Dev

Play framework routes, and scala predef values

From Dev

Play Framework 2.3.4 routes redirection to subproject

From Dev

Non-capturing wildcards in Play Framework routes

From Dev

Play framework - call controller in routes with Class parameter

From Dev

How to "rewrite" urls or routes with Play Framework 2

From Dev

Play Framework 2.3.4 routes redirection to subproject

From Dev

Error passing parameters in play framework

From Dev

Can not access parameters in play framework

From Dev

Post method confusion in play framework

From Dev

Bounded generic parameters in Play Framework template

Related Related

  1. 1

    Using POST routes parameters in Play Framework

  2. 2

    Play Framework: POST request with parameters

  3. 3

    how to define optional path parameters in Play Framework routes

  4. 4

    Play framework routes

  5. 5

    play framework - bind enum in routes

  6. 6

    Play Framework, REST, Routes, and Controllers

  7. 7

    Scala backticks in Play Framework Routes

  8. 8

    Domain routes in Play Framework 2.3

  9. 9

    Play Framework Error in Routes file

  10. 10

    Using parameters in Laravel routes

  11. 11

    play framework- how to send post request using java code in play framework

  12. 12

    play framework- how to send post request using java code in play framework

  13. 13

    Send POST request with a body using scala and play framework

  14. 14

    Non-capturing wildcards in Play Framework routes

  15. 15

    Iterating all Play Framework routes in Test

  16. 16

    split routes in multiple files for play framework 2.2

  17. 17

    Long dynamic routes in play framework 2

  18. 18

    Play Framework @routes.Assets.at Compilation Error

  19. 19

    Play framework - call controller in routes with Class parameter

  20. 20

    Play framework routes, and scala predef values

  21. 21

    Play Framework 2.3.4 routes redirection to subproject

  22. 22

    Non-capturing wildcards in Play Framework routes

  23. 23

    Play framework - call controller in routes with Class parameter

  24. 24

    How to "rewrite" urls or routes with Play Framework 2

  25. 25

    Play Framework 2.3.4 routes redirection to subproject

  26. 26

    Error passing parameters in play framework

  27. 27

    Can not access parameters in play framework

  28. 28

    Post method confusion in play framework

  29. 29

    Bounded generic parameters in Play Framework template

HotTag

Archive