swift - iOS - Alamofire v2 Basic Auth not working -


so i'm sending basic auth request bing image search grab image data, , working great, right until updated latest version of alamofire (1.3 -> 2.0.2), had because 1.3 wasn't close compatible xcode 7.

anyway, here code:

let credentials = ":\(settings.bingapikey)" let plaintext = credentials.datausingencoding(nsutf8stringencoding, allowlossyconversion: false) let base64 = plaintext!.base64encodedstringwithoptions(nsdatabase64encodingoptions(rawvalue: 0))  manager = alamofire.manager.sharedinstance manager!.session.configuration.httpadditionalheaders = [     "authorization": "basic \(base64)" ]  let url = nsurl(string: settings.bingimageapi + "&query=" + keyword + "&$top=15&$skip=" + string(skip))!  manager!   .request(.post, url, parameters: nil, encoding: .json)   .responsejson { request, response, result in       ... 

and i'm getting error:

failure: error domain=nscocoaerrordomain code=3840 "invalid value around character 0." userinfo={nsdebugdescription=invalid value around character 0.} the authorization type provided not supported. basic , oauth supported

i had same issue while moving alamofire 1.x 2.x.

one workaround found (and works), pass headers when performing request:

let headers = ["authorization": "basic \(base64)"] alamofire.request(.post, url, parameters: nil, encoding: .json, headers: headers) 

for more information can take @ documentation.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -