1

I have the following url : http://localhost/api/books/?bookId=21&bookId=62?authorId=2

But how i can retrieve all the bookId values with Scala ?

I'm using the PlayFrameWork as the WebServer, so here's my code :

val params = request.queryString.map { case (k, v) => k -> v(0) }

System.out.print(params.get("bookId"));

params.get("bookId") only get the last value in the bookId params. e-g : 62.

How can i retrieve all my bookId params ? Once i know how, i will be able to convert them into Integers.

Thanks,

user708683
  • 500
  • 3
  • 9
  • 26
  • 1
    Possible duplicate of [Have a List in Play framework web service parameters](http://stackoverflow.com/questions/35814866/have-a-list-in-play-framework-web-service-parameters) – marcospereira Apr 28 '16 at 21:55

1 Answers1

2

If you are willing to update your routes file, Play! can extract and transform duplicate Keys into an array for you:

GET    /api/books    controllers.Books.show(bookId: List[Int], authorId: Id)

will provide you with the parameter bookId: List[Int] to use in your call definition def show(bookId: List[Int], authorId: Int)

** UPDATE **

From the Play spec, it should:

"support several query string values for a parameter"

request.queryString returns a Map[String, Seq[String]]

The problem is you're transforming that into a Map[String, String] with:

val params = request.queryString.map { case (k, v) => k -> v(0) }

v(0) takes the first occurrence of each parameter

Just use the queryString Map directly: for the request /api/books/?bookId=21&bookId=62?authorId=2 request.queryString.get("bookId") will return a Option[Seq[String]] of all the bookId values which you can iterate over:

request.queryString.get("bookId").map { bookIds: Seq[String] => *do something with book ids here * }
tgk
  • 3,857
  • 2
  • 27
  • 42
  • There's away to avoid changing the routes and just retrieve the values from the string params ? Like it's the case with one param ? e-g authorId ? Because i will use the BookId params to fetch in a database using the IN clause. Thanks – user708683 Apr 29 '16 at 13:34
  • Can i have only Map[String, Seq[String]] then and use it when i don't have an array also ? eg : val params = request.queryString.map { case (k, v) => k -> v } and then get the values this way : params.get('bookId') and params.get('authorId') ? Cause at the end i want to have something like this : (a.authorId === params.get("authorId").?) and (params.get("bookId").map(bookIds: Seq[String] => b.bookId in bookIds).getOrElse(1 === 1).inhibitWhen(false)) – user708683 May 03 '16 at 11:58
  • That doesn't look like scala -- is this a SQL DSL? Maybe you could post as a new question, ideally (if i'm following) what you have is your own repository interface layer and you could query for an author and a List of bookIds, and that List could be of length 1+ – tgk May 03 '16 at 16:03
  • I'm using Scala and Squeryl. The goal is to get the values of authorId and BookId and fetch them in the Database : where (a.authorId === params.get("authorId").?) and (params.get("bookId").map(b => b.bookIds in MyListofBookIds))) – – user708683 May 03 '16 at 16:15