我正在使用Vapor的客户机获取GET请求。
func sendGetRequest(req: Request) throws -> Future<Response> {
let client = try req.make(FoundationClient.self)
return client.get("http://example.vapor.codes/json", headers: ["Accept-Language" : "ar"])
.map(to: Response.self, { clientResponse in
let response = req.makeResponse()
response.http.status = clientResponse.http.status
response.http.body = clientResponse.http.body
return response
})
}
这将返回所有的JSON数据,我希望对其进行过滤,以仅返回2个属性,例如在本例中(dict,number)
我已经为数据创建了一个模型
struct ExampleData: Codable {
// var array : [Int]
var dict : [String : String]
var number : Int
// var string : String
}
函数希望我返回一个future,但如果我将其更改为future并将映射设置为.map(to:exampledata.self..)
我得到
无法将“response”类型的返回表达式转换为返回类型
'TodoController.ExampleData'
最佳答案
我想出来了
func sendGetRequest(req: Request) throws -> Future<ExampleData> {
let client = try req.make(Client.self)
let ans = client.get("http://example.vapor.codes/json", headers: ["Accept-Language" : "ar"]).flatMap { exampleResponse in
return try exampleResponse.content.decode(ExampleData.self)
}
return ans
}