I recently migrated my project to Swift 2.0 and got lots of errors. I fixed most of them, but this one keeps making an error.
I am calling Bing Search API using Alamofire as below and I get an error saying "Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}"
I understand this means that the authentication fails. Could anybody advise me on how to fix this?
let percentedKeyword = searchKey.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
Let ulrStr: String = "https://api.datamarket.azure.com/Bing/Search/v1/News" + "? Query=" + percentedKeyword! + "&$top=10&$format=JSON"
let credentials = ":\(bingApiKey)"
let plainText = credentials.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let base64 = plainText!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
let headers = ["Authorization": "Basic \(base64)"]
Alamofire.request(.GET, urlStr, headers: headers)
.responseJSON { request, response, data in
switch data {
case Result.Success(let receivedValue):
self.bingJson = JSON(receivedValue)
case Result.Failure(_, let error as NSError):
print(error)
default:
print("do nothing")
}
}
Xcode version 7.0 Alamofire version 2.0.2
[Update]
I tried urlStr("https://api.datamarket.azure.com/Bing/Search/News?Query=%E4%B8%AD%E5%9B%BD&$top=10&$format=JSON") the web browser. It asked me to type in user name and password, so I typed apiKey as password and kept the user name blank following the instruction by Microsoft doc
I got this error:Parameter: Query is not of type String
According to stackoverflow, this is because keyword is not percented, but I am adding percent to keyword string...