ios - Read JSON from URL with authentication -
i try read json url, authentication didn't work. found al lot of examples in net, every time authentication fails...
this ist actuelly code :
let requesturl: nsurl! = nsurl(string: "http://example.com")! let request = nsmutableurlrequest(url:requesturl!); let username = "username" let password = "pw" let loginstring = nsstring(format: "%@:%@", username, password) let logindata: nsdata = loginstring.datausingencoding(nsutf8stringencoding)! let base64loginstring = logindata.base64encodedstringwithoptions(nsdatabase64encodingoptions()) request.setvalue(base64loginstring, forhttpheaderfield: "authorization") let task = nsurlsession.sharedsession().datataskwithrequest(request) { (data, response, error) -> void in let httpresponse = response as! nshttpurlresponse let statuscode = httpresponse.statuscode print(statuscode) } task.resume()
any ideas why didn't work? :)
t-jey
make sure method request post
//post method request.httpmethod = "post" //base64 needs basic keyword in auth header request.setvalue("basic \(base64loginstring)", forhttpheaderfield: "authorization")
Comments
Post a Comment