Get array of one url param
Asked Answered
C

4

8

I want get the url-param ids, but It will not work. Is here everyone who can help me? The following code doesn't work.

Url:

http://localhost:9000/rest/alerts?ids[]=123?ids[]=456

Routes.conf

GET /restws/alerts{ids} controllers.AlertService.findAlertsForIds(ids: List[String])

AlertService.java

public static Result findAlertsForIds(List<String> ids){

 return ok("Coole Sache"); 

 }
Catsup answered 18/6, 2013 at 8:42 Comment(0)
F
1

Try passing ids as string like this http://<hostname>:9000/rest/alerts?ids=123,456,789

and then get the array by applying the split() function on the string.

Hope it helps.

Fenton answered 18/6, 2013 at 8:51 Comment(0)
P
32

This kind of parameter binding works out-of-the box with query-string parameters.

Your route have to be declared like this :

GET /restws/alerts controllers.AlertService.findAlertsForIds(ids: List[String])

And your URL should follow this pattern :

http://localhost:9000/rest/alerts?ids=123&ids=456
Pita answered 18/6, 2013 at 14:7 Comment(0)
G
2

In short, you've got a lot of options... all a little different from each other. The following URL formats can be used:

  • /foo/bar?color=red&color=blue
  • /foo/bar?color=red,blue
  • /foo/bar?color=[red,blue]
  • /foo/bar?color[]=red,blue

You can either:

1. Use Play Forms

Simple config:

// form definition
case class ColorParams(colors:Seq[String])

val myForm = Form(formMapping(
  "color" -> seq(text(1, 32)))(ColorParams.apply)(ColorParams.unapply)

// in your controller method call
val params = myForm.bindFromRequest()

Example URL: /foo/bar?color[]=red,blue will become List("red","blue")

Sadly this isn't as robust, as many API's use the format color=red,blue or color=red&color=blue

2. Create your own custom query string parser

More elaborate, but you can write constraints, tests, and leave everything to the Router. Pro is that invalid queries never make it to your Controller.

Then you just use it in your Routes file like:

case class ColorParams(colors:List[MyColorEnum])

GET /foo/bar? controllers.Example.getBar(color:ColorParams)

Example URL: /foo/bar?color=red,blue

Because you're parsing the string yourself, you can many any string layout in this post work as desired with this method. See full details on QueryStringBindable. I'll add a full example later.

3. Define your array parameter in the Routes file

GET /foo/bar? controllers.Example.getBar(color:Seq[String])

// inside Example.scala
def getBar( colors:Seq[String] ) = {
    ???
}

Example URL: /foo/bar?color=red&color=blue

Note: I used color in the Routes file, which is the name in the URL, but colors in the getBar method, for clarity. These names don't have to match, just the types.

Gotchas

Note this can be tricky, because /foo/bar?color=red,blue becomes Seq('red,blue'), that is a Seq of one string, not two strings, but it will still display in the debugger as Seq(red,blue). The proper value to see in the debugger would be Seq(red, blue), notice that space? Tricky.

Glindaglinka answered 13/5, 2016 at 16:24 Comment(0)
F
1

Try passing ids as string like this http://<hostname>:9000/rest/alerts?ids=123,456,789

and then get the array by applying the split() function on the string.

Hope it helps.

Fenton answered 18/6, 2013 at 8:51 Comment(0)
R
0

Play 2.5:

// http://localhost:9000/myview?option=qwer=5&option=pass&option=43,56&otherOption=5

class MyController extends Controller  {
  def myview() = Action { implicit request =>
    println(request.queryString)

returns:

Map(option -> Buffer(qwer=5, pass, 43,56), otherOption -> Buffer(5))
Retributive answered 27/8, 2016 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.