osx - Coudn't load JSON while calling api in Swift -


i have own api application. trying call api , need json data response. here code

import cocoa  class viewcontroller: nsviewcontroller {  @iboutlet weak var emailtext: nstextfield!  @iboutlet weak var passwordtext: nssecuretextfield! @ibaction func signin(sender: anyobject) {     println("email \(emailtext.objectvalue) , password \(passwordtext.objectvalue)")      var url : string = "https:username:password@api.example.co/api/v3/auth/token/"     var request : nsmutableurlrequest = nsmutableurlrequest()     request.url = nsurl(string: url)     request.httpmethod = "get"      nsurlconnection.sendasynchronousrequest(request, queue: nsoperationqueue(), completionhandler:{ (response:nsurlresponse!, data: nsdata!, error: nserror!) -> void in         var error: autoreleasingunsafemutablepointer<nserror?> = nil         let jsonresult: nsdictionary! = nsjsonserialization.jsonobjectwithdata(data, options:nsjsonreadingoptions.mutablecontainers, error: error) as? nsdictionary          if (jsonresult != nil) {             println("\(jsonresult)")         } else {             println("couldn't load json")         }       }) } override func viewdidload() {     super.viewdidload()      // additional setup after loading view. }  override var representedobject: anyobject? {     didset {     // update view, if loaded.     } } } 

the jsonresult should give me apikey json data.

but else condition showing me while running mac app. new swift , building mac app, not able overcome this.

this duplicate of error: error domain=httptask code=404 "page not found" userinfo=0x608000228720 {nslocalizeddescription=page not found} think. anyways said question please check below.

you using format user@email.com:password@api.domain/com/.... works while working in javascript or jquery.

in swift, doesn't takes in basic-auth. basic auth, have send encode string authentication url like:

let config = nsurlsessionconfiguration.defaultsessionconfiguration()     let userpasswordstring = "\(email.stringvalue):\(password.stringvalue)"     let userpassworddata = userpasswordstring.datausingencoding(nsutf8stringencoding)     let base64encodedcredential = userpassworddata!.base64encodedstringwithoptions(nil)     let authstring = "basic \(base64encodedcredential)"     println("\(authstring)")     config.httpadditionalheaders = ["authorization" : authstring]     let session = nsurlsession(configuration: config)        let url = nsurl(string: "https://api.domain.com/api/v2/auth/token/")     let task = session.datataskwithurl(url!) {         (let data, let response, let error) in         if let httpresponse = response as? nshttpurlresponse {             let datastring = nsstring(data: data, encoding: nsutf8stringencoding) } } 

after print data , see meets question.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -