Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access query parameters in vapor 3

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.

like image 722
Jack Maloney Avatar asked Aug 21 '18 17:08

Jack Maloney


People also ask

What is query parameter key?

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.


1 Answers

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
}
like image 130
joscdk Avatar answered Oct 17 '22 02:10

joscdk