В нашем случае шлюз API настроен на запрос ключа API и токена пользователя. Таким образом, мы передаем x-api-key
и x-pool-token
в заголовке. Вот реализация со встроенной библиотекой Foundation:
guard let url = URL(string: "https://your-api-gateway-url") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
var headerParameters: [String: String] = [
"Content-Type": "application/json", // could be different, depends on your needs
"Accept": "application/json", // could be different, depends on your needs
"x-api-key": "your API key here" // API key, if Gateway is configured to request it
"x-pool-token": "current valid user token here" // valid auth token from the logged in user
]
request.allHTTPHeaderFields = headerParameters
request.httpBody = //... (encoded body)
URLSession.shared.dataTask(with: request) { (data, response, err) in
// process response here