https://kotlinlang.org logo
Title
d

Danish Ansari

08/04/2021, 5:50 AM
Hi, I would like to receive
List<Stirng>
in a
GET
request what's the most efficient way to do so? I'm currently doing like this but was hoping if I can receive List<String> without any manual operation
val keywords = call.request.queryParameters["keywords"]?.split(',') // List<String>?
GET
url will be like
?keywords=a,b,c
ł

Łukasz Bednarczyk

08/04/2021, 6:19 AM
You can try to use Location and set List<String> as a param
d

Danish Ansari

08/04/2021, 6:20 AM
Sorry I'm quite new and didn't understand what you just said. Can you show some small example or redirect to some doc regrading the same?
ł

Łukasz Bednarczyk

08/04/2021, 6:20 AM
Locations | Ktor 🙂
👍 1
Do you understand how does it work?
d

Danish Ansari

08/04/2021, 6:26 AM
Thanks
figuring out
b

Big Chungus

08/04/2021, 6:53 AM
Also call.request.queryParameters.getAll("key")
Then you make a request by passing in multiple key=one&key=two...
d

Danish Ansari

08/04/2021, 6:55 AM
@Big Chungus thanks but wouldn't that make things hefty for the client?
b

Big Chungus

08/04/2021, 6:56 AM
Well it's a standard http way of defining areay-like query parameters
1
Plus remember that client will be building query in code, so it can just use loop or map function to prefix stuff with key
d

Danish Ansari

08/04/2021, 6:57 AM
ok thanks, well then I think I'll go by
getAll
method because I don't I think I should add new plugin just for such a simple use case. right?
👍 1
b

Big Chungus

08/04/2021, 6:57 AM
Ktor might even support comma separated params with getAll. Give it a go, I haven't tried it myself that way
d

Danish Ansari

08/04/2021, 6:59 AM
NO comma separated didn't work, but the way you mentioned worked
👍 2
b

Big Chungus

08/04/2021, 7:00 AM
Good to know!
👍 1
m

Matteo Mirk

08/13/2021, 8:23 AM
@Danish Ansari the client, unless is someone using curl or similar, should be using an appropriate library that will abstract away the complexity of building a URL query string