2 Replies
      Latest reply: Dec 6, 2016 1:08 AM by nitisha RSS
      nitisha Level 1 Level 1 (0 points)

        Hi,

        I'm getting strange behaviour when posting to get response from server which requires Basic Authorization.my token gets gets expired after every two hours.Every things works fine but some times i dn't knw but getting reponse from server "Parameter not null" error.i debuged there is nothing going null.i go through document-https://developer.apple.com/reference/foundation/nsmutableurlrequest

        where mentioned some http headers could not be modified using methods

        addValue(_:forHTTPHeaderField:) or setValue(_:forHTTPHeaderField:)

        i cannot figure where is going wrong and more when i'm setting Content & Accept type for json addValue(_:forHTTPHeaderField:) workds gud to set authorization and for xml  setValue(_:forHTTPHeaderField:) works but still .sometimes  they breaks.Below is my code

        //for json rsponse
        func callWebserviceForJSON(_ urlStr: String ,callback:@escaping (_ data: Dictionary<String,String>) -> Void)
            {
                  
                let url1 = URL(string: urlStr)
                var request = URLRequest(url: url1!)
              
                 let tokenId = fileMgr.getCacheData(constants.defaultsKeys.TOKEN_KEY) as! String
              
                let authorizationKey = "Basic ".appending(tokenId)
                request.httpMethod = "POST"
                request.addValue("application/json", forHTTPHeaderField: "Content-Type")
                request.addValue("application/json", forHTTPHeaderField: "Accept")
                request.addValue(authorizationKey, forHTTPHeaderField: "Authorization")
                request.timeoutInterval = 20000.0
              
                var dict = Dictionary<String,String>()
                let task = URLSession.shared.dataTask(with: request)
                  {
                    data, response, error -> Void in
             }
        //for xml response
        
        /
            func callPostWebservice(_ urlStr: String, callback:@escaping (_ data: Dictionary<String,String>) -> Void)
            {
               
                 
               
                let url1 = URL(string: urlStr)
                var request = URLRequest(url: url1!)
               
                 let tokenId = fileMgr.getCacheData(constants.defaultsKeys.TOKEN_KEY) as! String
               
                print("tokenId >> \(tokenId) , urlStr >> \(urlStr)")
                request.httpMethod = "POST"
                request.addValue("application/xml", forHTTPHeaderField: "Content-Type")
                request.addValue("application/xml", forHTTPHeaderField: "Accept")
                request.setValue("Basic \(tokenId)", forHTTPHeaderField: "Authorization")
                request.timeoutInterval = 20000.0
               
                var dict = Dictionary<String,String>()
                let task = URLSession.shared.dataTask(with: request)
                {data, response, error  in }
        

        Please advice

        • Re: How to set Authorization to URlRequest in swift 3.0
          eskimo Apple Staff Apple Staff (6,005 points)

          I’m not sure what’s going on with your specific code but setting the Authorization header manually is not the right approach.  Rather, you should set up your session to handle authentication challenges via the -URLSession:task:didReceiveChallenge:completionHandler: delegate callback.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"