parameter with list of values

20 views
Skip to first unread message

Corentin Dupont

unread,
Aug 14, 2019, 5:52:37 AM8/14/19
to haskell-servant
Hi guys,
I hope you are doing well.
I want to parse a parameter that accepts a list of values:


I tried:

:> QueryParam "foo"  [Foo]

But it complains:

No instance for (FromHttpApiData [Foo])

I could try to provide this instance of FromHttpApiData, and write a parser for parameters value list, but I was wondering if there is some kind of support already in Servant? Any example?

Thanks a lot and good vacations!
Corentin

Arian van Putten

unread,
Aug 14, 2019, 7:30:15 AM8/14/19
to Corentin Dupont, haskell-servant
Hey,



:> QueryParams "foo" Foo

The syntax will be:
 ?foo[]=<foo1>&foo[]=<foo2>&...

The [] part is actually ignored, so this will work too:

?foo=<foo1>&foo=<foo2>&..

The comma-separated syntax we currently do not support I think

--
You received this message because you are subscribed to the Google Groups "haskell-servant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskell-serva...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/haskell-servant/CAEyhvmrcjbzXJ3W0%3D83sqmGWSbkSj0%2BTZe31symEcMyV91X_Yw%40mail.gmail.com.


--
Groetjes,

Arian

Alp Mestanogullari

unread,
Aug 14, 2019, 10:43:19 AM8/14/19
to Corentin Dupont, haskell-servant
There isn't support for this in servant, as (as far as I know) this comma-separated format isn't really standard. You could however indeed provide a general combinator + {From,To}HttpApiData instances.

newtype CommaSep a = CommaSep [a]
instance FromHttpApiData a => FromHttpApiData (CommaSep a) where ...
instance ToHttpApiData a => ToHttpApiData (CommaSep a) where ...

where the From instance would split on each comma character and try to parse every "component" using the supplied FromHttpApiData instance for 'a'.

If you don't care about the syntax/etc, then Arian's QueryParams suggestion would probably be the simplest solution.


On Wed, Aug 14, 2019 at 11:52 AM Corentin Dupont <corenti...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "haskell-servant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to haskell-serva...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/haskell-servant/CAEyhvmrcjbzXJ3W0%3D83sqmGWSbkSj0%2BTZe31symEcMyV91X_Yw%40mail.gmail.com.


--
Alp Mestanogullari
Reply all
Reply to author
Forward
0 new messages