1

I'd like to manipulate the following curl into swift.I have no idea how to write header of NSURLSession in 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

This is my code, however it returned error at the end of sentence. Could you tell me what is the problem?

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.)"}

Toshi
  • 1,293
  • 3
  • 19
  • 37

1 Answers1

0

I've solved the problem as following. Thank you for your help.

func exchangerate(){
let token = "abcdef"
var buyCurrency = "EUR"
var sellCurrency = "USD"
var fixSide = "buy"
var amount = "1000"
// let request = NSMutableURLRequest(URL: NSURL(string: "https://devapi.thecurrencycloud.com/v2/rates/detailed")!)
var buyCurrency = "EUR"
//var postString:NSString = "buy_currency=\(buyCurrency)&sell_currency=\(sellCurrency)&fixed_side=\(fixSide)&amount=\(amount)"
let request = NSMutableURLRequest(URL: NSURL(string: "https://devapi.thecurrencycloud.com/v2/rates/detailed?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()

}

Toshi
  • 1,293
  • 3
  • 19
  • 37