我正在使用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
我觉得我已经在互联网上搜索了很多关于这个主题的文档,但都找不到。
有没有人有任何关于查询字符串的资源或建议可以分享?
发布于 2017-01-17 13:02:47
生成查询字符串的最简单方法是使用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
}https://stackoverflow.com/questions/41689152
复制相似问题