title: Calling a secure adapter returns Response Error 401
body: <p>I am trying to call a secure adapter in my watch kit application. But my requests fails with the following error message in output window in Xcode.</p>
  
  <pre><code>Request url is http://:10080/UProject/adapters/MyAdapter/getMyBalance
  2015-12-28 21:49:29.222 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper start] in WLAFHTTPRequestOperationManagerWrapper.m:320 :: Starting the request with URL http://:10080/UProject/adapters/MyAdapter/getMyBalance?param=700015005
  2015-12-28 21:49:29.222 My WatchKit 1 Extension[59266:1304116] After adapter Request
  2015-12-28 21:49:29.247 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper requestFailed:error:] in WLAFHTTPRequestOperationManagerWrapper.m:352 :: Request Failed
  2015-12-28 21:49:29.247 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper requestFailed:error:] in WLAFHTTPRequestOperationManagerWrapper.m:353 :: Response Status Code : 401
  2015-12-28 21:49:29.248 My WatchKit 1 Extension[59266:1304116] [DEBUG] [WL_AFHTTPRequestOperationManagerWrapper_PACKAGE] -[WLAFHTTPRequestOperationManagerWrapper requestFailed:error:] in WLAFHTTPRequestOperationManagerWrapper.m:354 :: Response Error : Request failed: unauthorized (401)
  
  
  
  
  //WKInterfaceController
  import WatchKit
  import Foundation
  //import IBMMobileFirstPlatformFoundation
  
  
  class InterfaceController: WKInterfaceController {
  
  @IBOutlet var btnLoadFeeds: WKInterfaceButton!
  @IBOutlet var btnConnect: WKInterfaceButton!
  
  var challengeHandler : ChallengeHandler?
  
  
  @IBOutlet var feedsTable: WKInterfaceTable!
  
  var feeds = []
  
  
  override func awakeWithContext(context: AnyObject?) {
      super.awakeWithContext(context)
  
      // Configure interface objects here.
  
    btnLoadFeeds.setEnabled(false)
  
     let connectListener = MyConnectListener()
     WLClient.sharedInstance().registerChallengeHandler(MyChallengeHandler(vc: self))
     WLClient.sharedInstance().wlConnectWithDelegate(connectListener)
  
     }
  
  
  override func willActivate() {
      // This method is called when watch view controller is about to be visible to user
      super.willActivate()
  }
  
  override func didDeactivate() {
      // This method is called when watch view controller is no longer visible
      super.didDeactivate()
  }
  
  
  
  @IBAction func btnConnectClicked() {
  
      let url : NSURL = NSURL(string: "/adapters/SecureAdapter/getBalance")!
      let request : WLResourceRequest = WLResourceRequest(URL: url, method: WLHttpMethodGet)
  
      request.setQueryParameterValue("700015005", forName: "param")
  
  
      request.sendWithCompletionHandler {
  
          (response: WLResponse!, error: NSError!) -&gt; Void in
  
          if error != nil
          {
              NSLog("Adapter invocation failure. Error: %@", error.description)
          } else
          {
             NSLog(response.responseText)
          }
  
      }
      NSLog("After adapter Request")
  
  
  }
  
  
  @IBAction func btnLoadClicked() {
      btnConnect.setHidden(true)
      btnLoadFeeds.setHidden(true)
  
      if (feeds.count &gt; 0 )
      {
      feedsTable.setNumberOfRows(feeds.count, withRowType: "FeedRowController")
  
  
      for (index,feedItem) in feeds.enumerate()
      {
       let row = feedsTable.rowControllerAtIndex(index) as! FeedRowController
          row.labelText.setText(feedItem["title"] as? String)
          print(feedItem)
  
      }
  
      }
  
  
  }
  
  
  func onFailure(response: WLFailResponse!) {
      print(" failure")
  }
  
  
  func onSuccess(response: WLResponse!) {
      print("success")
      print(response.responseText)
  
      feeds = response.getResponseJson()["Items"] as! NSArray
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>//MyChallengeHandler<br>
  import Foundation</p>
  
  <p>class MyChallengeHandler: ChallengeHandler {</p>
  
  <pre><code>var challengeHandler : ChallengeHandler?
  
  let vc : InterfaceController
  
  init(vc: InterfaceController){
      self.vc = vc
      super.init(realm: "LDAPRealm")
  }
  
  
  
  override func isCustomResponse(response: WLResponse!) -&gt; Bool {
  
  
      if response != nil &amp;&amp; response.responseText != nil{
          if response.responseText.lowercaseString.rangeOfString("j_security_check") != nil{
              NSLog("Detected j_security_check string - returns true")
  
              return true
          }
      }
      return false
  }
  
  override func handleChallenge(response: WLResponse!) {
      NSLog("handleChallenge")
      vc.challengeHandler = self
      dispatch_async(dispatch_get_main_queue()) {
      self.challengeHandler?.submitLoginForm("j_security_check", requestParameters: ["j_username": "demouser", "j_password": "12345678"], requestHeaders: nil, requestTimeoutInMilliSeconds:0, requestMethod: "POST")
      }
  
  }
  
  override func onSuccess(response: WLResponse!) {
      NSLog("zaq3 Challenge succeeded")
           self.submitSuccess(response)
  }
  
  override func onFailure(response: WLFailResponse!) {
      NSLog(" zaq3 Challenge failed")
      self.submitFailure(response)
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Can anyone point what  I am missing in the code.  </p>