I’m getting data using this API: Recipe - Food - Nutrition API Documentation (spoonacular) | RapidAPI
In this example, I’m using a GET
method for “Extract Recipe from Website.”’
This is what I have in my viewDidLoad()
:
let headers = [
"x-rapidapi-host": "spoonacular-recipe-food-nutrition-v1.p.rapidapi.com",
"x-rapidapi-key": "1234567890qwertyuiopasdfghjklzxcvbnm"
]
let request = NSMutableURLRequest(url: NSURL(string: "https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/extract?url=http%3A%2F%2Fwww.melskitchencafe.com%2Fthe-best-fudgy-brownies%2F")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
// Parse out the data
do {
let dictionary = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? [String:Any]
print("DICTIONARY: \(dictionary)")
}
catch {
print("Error parsing response data")
}
}
})
dataTask.resume()
When I view the breakpoint at print("Error parsing response data")
, I can see that I’ve successfully parsed the data. For example:
But once I’ve done that parsing, how can I actually use those key-value pairs? For example if I wanted to extract the value from the key of “id”?
Any help is much appreciated!