如何在 Vapor 路由处理程序中处理空查询参数

问题描述 投票:0回答:1

我想处理这个 HTTP GET 请求 http://localhost:8080/route?wsdl 并返回一个静态文件

我已经开始使用这个路由处理程序 app.get("route") { req throws -> EventLoopFuture in

      // req.query.get(String.self, at: "wsdl") throws an DecodingError
      // I would like to check if the empty query parameter wsdl is present
      // once I can check for the empty query parameter, I would use this for an if  
      //statement and in the case it is present return a static file
      // then only a static file must be returned
        return req.eventLoop.makeSucceededFuture(
            req.fileio.streamFile(at: "file.wsdl") 
          )
    }
}

我也尝试过这些路线,但没有成功: app.get("路线?wsdl") -> 这没有被调用,可能不应该工作

app.get("路线",":wsdl") -> 这没有被调用

swift vapor
1个回答
0
投票
// route handler   
app.get("route") { req throws -> EventLoopFuture<Response> in
        if let query = req.url.query,
            query.count == 4,
           query == "wsdl" {
            return req.eventLoop.makeSucceededFuture(
                req.fileio.streamFile(at: "file.wsdl")
              )
            }
        else {
            throw Abort(.notFound)
        }
   }
© www.soinside.com 2019 - 2024. All rights reserved.