deniskakacka deniskakacka - 20 days ago 14
Swift Question

Alamofire Xcode 8 GM, Missing Mashape application key

I can't solve this problem for about 2 days now, I want data from Food2Fork API, but still have an error "Missing Mashape application key" but I have Mashape Key in header.. Maybe there is something bad with my request.

Constants.swift

let API_KEY = "42d72354227d6e455679488ce2dceb55"
let SEARCH_URL = "https://community-food2fork.p.mashape.com/search?key=\(API_KEY)&q="
let X_MASHUP_KEY = "TsLaXgXz5tmshzlqACubx3q86GV5p1rs1rSjsn04mIJoMoZYjl"


Receipt.swift

func downloadRecipes(completed: DownloadComplete) {

let recipesURL = URL(string: "\(SEARCH_URL)chicken")

Alamofire.request(recipesURL!, method: .get, headers: ["\(X_MASHUP_KEY)": "\(API_KEY)", "Accept application" : "json"]).responseJSON() { response in
let result = response.result
print(result)
}
completed()
}

Answer

This is how I solved this:

func searchRecipes(completed: DownloadComplete, ingredients: String) {

    let url = URL(string: "\(SEARCH_URL)\(ingredients)")
    let headers = ["X-Mashape-Key": "1N5g1M5d4RmshwjNve3IRDyjqGGAp1GDwubjsnorfFstlon4qi"]

    Alamofire.request(url!, method: .get, headers: headers).validate().responseJSON() { response in
        let result = response.result

        if let dict = result.value as? Dictionary<String, AnyObject> {

            if let recipes = dict["recipes"] as? [Dictionary<String, AnyObject>] {

                for obj in recipes {
                    let recipes = Recipe(recipesDict: obj)
                    self.recipes.append(recipes)
                    print(obj)
                }
                self.tableView.reloadData()
                print(recipes.count)
            }

        }
    }
    completed()

}
Comments