以下の curl
のコマンドを Swift で実現したいです。しかしヘッダ部分の記述方法がわかりません。
curl -X GET \
--header "X-Auth-Token: abcdef" \
https://devapi.thecurrencycloud.com/v2/rates/detailed?buy_currency=EUR&sell_currency=USD&fixed_side=buy&amount=1000
こちらが私のコードです。しかしエラーとなってしまいました。解決方法をご教示頂きたいです。
func exchangerate(){
let token = "abcdef"
let request = NSMutableURLRequest(URL: NSURL(string: "https://devapi.thecurrencycloud.com/v2/rates/detailed")!)
var buyCurrency = "EUR"
var sellCurrency = "USD"
var fixSide = "buy"
var amount = "1000"
var postString:NSString = "buy_currency=\(buyCurrency)&sell_currency=\(sellCurrency)&fixed_side=\(fixSide)&amount=\(amount)"
request.HTTPMethod = "GET"
request.setValue(token, forHTTPHeaderField: "X-Auth-Token")
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
println("response = \(response)")
let responseStringExchangeRate = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseStringExchangeRate = \(responseStringExchangeRate)")
}
task.resume()
}
エラー:
error=Error Domain=NSURLErrorDomain
Code=-1005
"The operation couldn’t be completed. (NSURLErrorDomain error -1005.)"
UserInfo=0x7fea7b4d63f0
{
NSErrorFailingURLStringKey=https://devapi.thecurrencycloud.com/v2/rates/detailed,
NSErrorFailingURLKey=https://devapi.thecurrencycloud.com/v2/rates/detailed, _kCFStreamErrorDomainKey=4,
_kCFStreamErrorCodeKey=-4,
NSUnderlyingError=0x7fea7d805ba0
"The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1005.)"
}