【问题标题】:How to access query parameters in vapor 3如何访问 Vapor 3 中的查询参数
【发布时间】:2018-08-21 17:49:01
【问题描述】:

基本上是标题。我想知道如何在 Vapor 3 中使用 url 查询参数。我似乎在它的文档中找不到任何东西。

例如/objects?fancy=true,如何访问fancy参数。

【问题讨论】:

    标签: swift rest query-parameters vapor


    【解决方案1】:

    你可以做类似的事情,例如:

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

    或者如果它是可选的,你可以这样做

    if let qFancy = try? req.query.get(Bool.self, at: "fancy") {
        fancy = qFancy
    } else {
        fancy = false
    }
    

    【讨论】:

    • 嗨!如果 GET 参数是可选的,有时发送有时不发送怎么办?如何检查存在?
    猜你喜欢
    • 2018-09-04
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2021-05-04
    • 1970-01-01
    • 2019-04-01
    相关资源
    最近更新 更多