I tried to use "Yoda Speak" API by Mashape. I got binary data but I don't know how to parse the data. When I try to print the data, I got this message. "(message, Missing Mashape application key. Go to https://www.mashape.com to get your key.)" I think the Mashape application key is "jY0bEhHCBpmsh8j1mpA5p11tCJGyp1tok3Zjsn4ubbvNNp5Jt3".
How to adapt this key in Swift?
func response(res: NSURLResponse!, data: NSData!, error: NSError!) {
let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil) as NSDictionary
let header: NSDictionary = ["X-Mashape-Key" : "jY0bEhHCBpmsh8j1mpA5p11tCJGyp1tok3Zjsn4ubbvNNp5Jt3"]
for value in json {
println(value)
}
}
func getData() {
let url = NSURL(string: "https://yoda.p.mashape.com/yoda?sentence=I+like+you")!
let req = NSURLRequest(URL: url)
let connection: NSURLConnection = NSURLConnection(request: req, delegate: self, startImmediately: false)!
NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue(), completionHandler: response)
}
You need to set the X-Mashape-Key
as a header to your NSURLRequest
. To do so:
let req = NSMutableURLRequest(URL: url)
req.setValue("jY0bEhHCBpmsh8j1mpA5p11tCJGyp1tok3Zjsn4ubbvNNp5Jt3", forHTTPHeaderField: "X-Mashape-Key")
NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue(), completionHandler: response)
Should do the trick. You can then remove the header NSDictionary
from your response handler as it is of no use.