0

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...

Community
  • 1
  • 1
Yuichi Kato
  • 863
  • 7
  • 16
  • You should switch over to use `responseString`. The JSON parsing is failing. – cnoon Oct 04 '15 at 20:32
  • You also have a space between your `?` and `Query` keyword in the query string. – cnoon Oct 04 '15 at 20:33
  • Thank you for your advice! It turns out that I needed single quotation marks for search key words. The space between ? and Query seems to have been created by my spell check plug-in (Ginger). The JSON parsing seems to be still working. – Yuichi Kato Oct 08 '15 at 05:25

1 Answers1

1

It turns out that I needed single quotation marks for search key words.

let percentedKeyword = searchKey.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
        let urlStr: String = "https://api.datamarket.azure.com/Bing/Search/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")
                }
        }
    }
Yuichi Kato
  • 863
  • 7
  • 16