I'm trying send an http get request with Alamofire using Yelp's business search API (https://www.yelp.ca/developers/documentation/v3/business_search) but I'm having trouble writing out the syntax. I'm a beginner and am using Alamofire for the first time so some help would be appreciated! I'm also trying to set "term" and "location" parameters. Once thats done, I'm hoping to parse through the SwiftyJSON and am not sure how/where to receive the response.
This is my current code:
import UIKit
import Alamofire
import SwiftyJSON
private let reuseIdentifier = "cafeCell"
class CafeListCollectionViewController:
UICollectionViewController,UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
let requestParams: Parameters = ["term": "cafe", "location": "Montreal, QC"]
//Http request
let apiToContact = "https://api.yelp.com/v3/businesses/search"
Alamofire.request(.GET, apiToContact, requestParams).responseJSON { (responseObject) -> Void in
print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error : NSError = responseObject.result.error!
failure(error)
}
}
I'm sorry if this sounds like a simple question. I'm new to programming and would appreciate the help. Thank you so much!
The problem might be that you're looking at a Swift 2 example but writing in Swift 3. Here's a Swift 3 version:
let requestParams: Parameters = ["term": "cafe", "location": "Montreal, QC"]
//Http request
let apiToContact = "https://api.yelp.com/v3/businesses/search"
Alamofire.request(apiToContact, method: .get, parameters: requestParams, encoding: URLEncoding.default, headers: nil).responseJSON { (responseObject) in
print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error : NSError = responseObject.result.error!
failure(error)
}
}
You will also need an API token from Yelp to get a valid response.