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.
The additional key-value pairs that occur after the question mark in the URL are referred to as API query parameters. They're essentially URL extensions that help decide certain content or actions based on the data being sent. A “?” is used to attach query parameters to the end of the URL.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With