我正在使用AlamoFire
发出API请求。连接到API非常简单,查询API非常具有挑战性。
我正在尝试创建一个与此类似的查询字符串:
https://api-fxtrade.oanda.com/v3/instruments/USD_CAD/candles?price=BA&from=2016-10-17T15%3A00%3A00.000000000Z&granularity=M1
我觉得我已经在很多互联网上搜索了关于这个主题的文档并且已经缩短了...
是否有人有任何资源或建议来分享查询字符串?
答案 0 :(得分:1)
创建查询字符串的最简单方法是使用URLComponents
,它可以为您处理所有百分比转义:
// Keep the init simple, something that you can be sure won't fail
var components = URLComponents(string: "https://api-fxtrade.oanda.com")!
// Now add the other items to your URL query
components.path = "/v3/instruments/USD_CAD/candles"
components.queryItems = [
URLQueryItem(name: "price", value: "BA"),
URLQueryItem(name: "from", value: "2016-10-17T15:00:00.000000000Z"),
URLQueryItem(name: "granularity", value: "M1")
]
if let url = components.url {
print(url)
} else {
print("can't make URL")
}
这就是纯粹的Swift,你应该熟悉它。掌握了基础知识后,Alamofire可以为您简化:
let params = [
"price": "BA",
"from": "2016-10-17T15:00:00.000000000Z",
"granularity": "M1"
]
Alamofire.request("https://api-fxtrade.oanda.com/v3/instruments/USD_CAD/candles", parameters: params)
.responseData { response in
// Handle response
}