【发布时间】:2018-11-25 06:02:00
【问题描述】:
我正在使用 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
我明白了
无法将“响应”类型的返回表达式转换为返回类型 'TodoController.ExampleData'
【问题讨论】:
-
可能是个愚蠢的问题,但您是否仍在返回您在
.map(to: ExampleData.self...中创建的Response对象?还是您将其更改为返回ExampleData结构? -
@ssrobbi 返回的类型为 ExampleData
标签: swift get client vapor server-side-swift