Get an array from a single URL parameter

I want to get url-param ids, but this will not work. Is there anyone here who can help me? The following code does not work.

Address:

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"); } 
+7
source share
4 answers

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

and then get the array by using the split () function in the string.

Hope this helps.

+1
source

This type of parameter binding works outside the box with query string parameters.

Your route should be declared as follows:

 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 
+27
source

In short, you have many options ... all are slightly different from each other. You can use the following URL formats:

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

You can:

1. Use forms of reproduction

Simple configuration:

 // 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")

Unfortunately, this is not so reliable, as many APIs use the format color=red,blue or color=red&color=blue

2. Create your own custom string parser

In more detail, but you can write restrictions, tests, and leave everything on the router. Pro is that invalid requests never reach your controller.

Then you just use it in your routes file, for example:

 case class ColorParams(colors:List[MyColorEnum]) 

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

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

Since you are parsing the string yourself, you can use any string layout in this post using this method. See QueryStringBindable for more details. I will add a complete 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. For clarity, I used color in the routes file, which is the name in the URL, but colors in the getBar method. These names should not match, only types.

Gotchas

Please note that this can be difficult, because /foo/bar?color=red,blue becomes Seq ("red, blue"), this is Seq of one line, not two lines, but it will still be displayed in the debugger as Seq(red,blue) . The correct value that you need to see in the debugger will be Seq(red, blue) , pay attention to this space? Tricky.

+2
source

Playback 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)) 
0
source

All Articles