title: Request Timeout NSURLSession
body: <p>hello I used this below code to send a request to Server. How can I add the timeout in this function </p>
  
  <pre><code>static func postToServer(url:String,var params:Dictionary&lt;String,NSObject&gt;, completionHandler: (NSDictionary?, String?) -&gt; Void ) -&gt; NSURLSessionTask {
  
  
          let request = NSMutableURLRequest(URL: NSURL(string: url)!)
  
  
          let session = NSURLSession.sharedSession()
  
          request.HTTPMethod = "POST"
  
      if(params["data"] != "get"){
          do {
  
              let data = try NSJSONSerialization.dataWithJSONObject(params, options: .PrettyPrinted)
  
              let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)!
              print("dataString is  \(dataString)")
  
              request.HTTPBody = data
  
  
          } catch {
              //handle error. Probably return or mark function as throws
              print("error is \(error)")
              //return
          }
  
      }
          request.addValue("application/json", forHTTPHeaderField: "Content-Type")
          request.addValue("application/json", forHTTPHeaderField: "Accept")
  
          let task = session.dataTaskWithRequest(request) {data, response, error -&gt; Void in
              // handle error
  
              guard error == nil else { return }
              request.timeoutInterval = 10
  
  
             print("Response: \(response)")
              let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
               completionHandler(nil,"Body: \(strData!)")
            //print("Body: \(strData!)")
  
              let json: NSDictionary?
              do {
                  json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
              } catch let dataError {
                  // Did the JSONObjectWithData constructor return an error? If so, log the error to the console
                  print(dataError)
                  let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print("Error could not parse JSON: '\(jsonStr)'")
                  completionHandler(nil,"Body: \(jsonStr!)")
  
                  // return or throw?
                  return
              }
  
  
              // The JSONObjectWithData constructor didn't return an error. But, we should still
              // check and make sure that json has a value using optional binding.
              if let parseJSON = json {
                  // Okay, the parsedJSON is here, let's get the value for 'success' out of it
  
                  completionHandler(parseJSON,nil)
                  //let success = parseJSON["success"] as? Int
                  //print("Succes: \(success)")
              }
              else {
                  // Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
                  let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                  print("Errors could not parse JSON: \(jsonStr)")
                  completionHandler(nil,"Body: \(jsonStr!)")
              }
  
          }
  
          task.resume()
          return task
      }
  </code></pre>
  
  <p>I did some search too and I came to know about to use this function </p>
  
  <pre><code>let request = NSURLRequest(URL: url!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0)
  </code></pre>
  
  <p>instead of this </p>
  
  <pre><code>let request = NSMutableURLRequest(URL: NSURL(string: url)!)
  </code></pre>
  
  <p>But problem is If I used the above function then I can't set these variables</p>
  
  <pre><code>request.HTTPBody = data
  request.addValue("application/json", forHTTPHeaderField: "Content-Type")
          request.addValue("application/json", forHTTPHeaderField: "Accept")
  </code></pre>
  
  <p>Please anyone suggest me correct solution of how to add timeout in my function</p>