我想用Vapor 3中的一些参数进行post调用。
POST: http://www.example.com/example/post/request
title: How to make api call
year: 2019
可以使用哪个包/函数?
发布于 2019-04-09 22:04:59
这很简单,您可以像这样使用Client
来完成此操作
func thirdPartyApiCall(on req: Request) throws -> Future<Response> {
let client = try req.client()
struct SomePayload: Content {
let title: String
let year: Int
}
return client.post("http://www.example.com/example/post/request", beforeSend: { req in
let payload = SomePayload(title: "How to make api call", year: 2019)
try req.content.encode(payload, as: .json)
})
}
或者例如在boot.swift
中像这样
/// Called after your application has initialized.
public func boot(_ app: Application) throws {
let client = try app.client()
struct SomePayload: Content {
let title: String
let year: Int
}
let _: Future<Void> = client.post("http://www.example.com/example/post/request", beforeSend: { req in
let payload = SomePayload(title: "How to make api call", year: 2019)
try req.content.encode(payload, as: .json)
}).map { response in
print(response.http.status)
}
}
https://stackoverflow.com/questions/55601797
复制相似问题