Это легко, вы можете сделать это, используя 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)
}
}