Retrieving all query parameters

455 views
Skip to first unread message

steve

unread,
Dec 21, 2011, 12:45:56 PM12/21/11
to Lift
Hey guys, having fun using lift, but I've got a question maybe
someone can answer. I've been looking all around and haven't found a
clear solution.

Is there an easy way in lift to get all the query parameters from a
URL? For example, if the URL is www.fruits.com/apple/orange/banana?f=1&q=bear&y=forty,

Is there a method that will generate a list of all the query
parameters. Like List( (f,1), (q,bear), (y,forty) ). I know i can
use S.param("f") so get a specific one, but I'm looking for away to
get all of them.

Once I have all of them, I'd like to add and remove from the list and
use something like HttpHelpers.appendQueryParameters to generate a url
with the new list of query parameters.

I hope that made sense. Please let me know if my question isn't
clear.

Thanks,
Steve

David Pollak

unread,
Dec 21, 2011, 1:05:36 PM12/21/11
to lif...@googlegroups.com
S.request.flatMap(_.params) // Box[Map[String, List[String]]

Steve

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net


steve

unread,
Dec 22, 2011, 10:20:38 AM12/22/11
to Lift
Hey David, Thanks for the response. This seems to be along the lines
of what I'm looking for, but I'm getting an error when I use it that
I'm struggling to figure out.

[error] found : Map[String,List[String]]
[error] required: net.liftweb.common.Box[?]
[error] val a = S.request.flatMap(_.params) // Box[Map[String,
List[String]]


Any insight?

Thanks!


On Dec 21, 1:05 pm, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> Visi.Pro, Cloud Computing for the Rest of Ushttp://visi.pro
> Lift, the simply functional web frameworkhttp://liftweb.net

steve

unread,
Dec 22, 2011, 10:35:43 AM12/22/11
to Lift
Oh and the ^ that shows where the error is supposed to be is directly
under the p. I'm going to work on this, but i appreciate help you
guys can give.

Richard Dallaway

unread,
Dec 22, 2011, 11:44:04 AM12/22/11
to lif...@googlegroups.com
On Thursday, 22 December 2011 at 15:20, steve wrote:
> Hey David, Thanks for the response. This seems to be along the lines
> of what I'm looking for, but I'm getting an error when I use it that
> I'm struggling to figure out.
>
> [error] found : Map[String,List[String]]
> [error] required: net.liftweb.common.Box (http://web.common.Box)[?]

> [error] val a = S.request.flatMap(_.params) // Box[Map[String,
> List[String]]
>
S.request is Box[Req], so it might be empty. What we can do is map over it, which I think of as "opening the box", applying a function, and putting the result back in the box:

val a : Box[Map[String,List[String]]] = S.request.map(_.params)

That might be what you want.

If you want to remove the Box, there's more work to do (because Box.flatMap expects a function that results in a Box, and req.params isn't in a Box). This looks plausible to me:

val b : Map[String,List[String]] = S.request.map(_.params).getOrElse(Map.empty)

Or, if you prefer, you can convert from Box to List:

val c : Map[String,List[String]] = S.request.toList.flatMap(_.params).toMap

S.request.toList gives you a (possibly zero length) List[Req] which is flatMapped to a List of pairs (String,List[String]) which you can turn back into a Map with toMap.

Hope that helps
Richard


steve

unread,
Dec 22, 2011, 4:00:35 PM12/22/11
to Lift
Thanks Richard, Your solution seemed to work exactly how I was
looking for it to! I have another question about creating arrays in
the url query string and then retrieving them back. I was told Ruby
has a simple way to do this. But I'll write up a new post about that
question.

Thanks a lot man!

David Pollak

unread,
Dec 22, 2011, 4:49:15 PM12/22/11
to lif...@googlegroups.com
On Thu, Dec 22, 2011 at 1:00 PM, steve <sgio...@manaproducts.com> wrote:
Thanks Richard,  Your solution seemed to work exactly how I was
looking for it to!  I have another question about creating arrays in
the url query string and then retrieving them back.  I was told Ruby
has a simple way to do this. But I'll write up a new post about that
question.


Before you write the question, please spend some time with Scala's map/flatMap.  Almost all the kinds of transforms you want to accomplish are variants of transforming collections.  map and flatMap (as well as foldLeft and filter) are generalizations of almost every transformation operation.  Spending time now with map/flatMap will pay you back 1,000 fold as you do more Scala stuff. 
 
Thanks a lot man!


On Dec 22, 11:44 am, Richard Dallaway <rich...@dallaway.com> wrote:
> On Thursday, 22 December 2011 at 15:20, steve wrote:
> > Hey David, Thanks for the response. This seems to be along the lines
> > of what I'm looking for, but I'm getting an error when I use it that
> > I'm struggling to figure out.
>
> > [error] found : Map[String,List[String]]
> > [error] required: net.liftweb.common.Box (http://web.common.Box)[?]
> > [error] val a = S.request.flatMap(_.params) // Box[Map[String,
> > List[String]]
>
> S.request is Box[Req], so it might be empty.  What we can do is map over it, which I think of as "opening the box", applying a function, and putting the result back in the box:
>
> val a : Box[Map[String,List[String]]] = S.request.map(_.params)
>
> That might be what you want.
>
> If you want to remove the Box, there's more work to do (because Box.flatMap expects a function that results in a Box, and req.params isn't in a Box).  This looks plausible to me:
>
> val b : Map[String,List[String]] = S.request.map(_.params).getOrElse(Map.empty)
>
> Or, if you prefer, you can convert from Box to List:
>
> val c : Map[String,List[String]] = S.request.toList.flatMap(_.params).toMap
>
> S.request.toList gives you a (possibly zero length) List[Req] which is flatMapped to a List of pairs (String,List[String]) which you can turn back into a Map with toMap.
>
> Hope that helps
> Richard

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net

steve

unread,
Dec 22, 2011, 5:16:56 PM12/22/11
to Lift
Thanks for the tip David. I'll experiment with map and flatmap. I
already posted my other question before reading your response. I'm
always willing to take as many suggestions as possible.



On Dec 22, 4:49 pm, David Pollak <feeder.of.the.be...@gmail.com>
wrote:
> Visi.Pro, Cloud Computing for the Rest of Ushttp://visi.pro
> Lift, the simply functional web frameworkhttp://liftweb.net
Reply all
Reply to author
Forward
0 new messages