0
我正在嘗試使用Zomato的API來獲取JSON數據的簡單獲取請求。我有一個API密鑰,但我不知道如何在正常的NSURLSession調用中使用它。我沒有提供用戶名或密碼,只有一個32位字符的API密鑰。Swift Api密鑰身份驗證
curl命令給出如下:
curl -X GET --header "Accept: application/json" --header "user_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" "https://developers.zomato.com/api/v2.1/search?entity_id=280&entity_type=city&count=5&cuisines=55"
我的請求代碼是在這裏:
let url = NSURL(string: myURL)!
let urlSession = NSURLSession.sharedSession()
//add api key to header somewhere here?
let myQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
//I have some error handling here
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary
let myArray:NSArray = jsonResult["restaurants"] as! NSArray
})
myQuery.resume()
謝謝,回答我的問題。 – PTerz