18

Basically the title. I want to know how to use url query parameters in Vapor 3. I can't seem to find anything in the docs on it.

e.g. /objects?fancy=true, how do I access the fancy parameter.

tanner0101
  • 4,005
  • 19
  • 33
Jack Maloney
  • 527
  • 2
  • 5
  • 18

1 Answers1

40

You can do something like e.g.:

guard let fancy = req.query[Bool.self, at: "fancy"] else {
    throw Abort(.badRequest)
}

Or if it's optional you could do

if let qFancy = try? req.query.get(Bool.self, at: "fancy") {
    fancy = qFancy
} else {
    fancy = false
}
joscdk
  • 494
  • 5
  • 3
  • Hi! What if GET param is optional, sometimes sent sometimes not? How can I check existence? – János Dec 18 '19 at 20:52