title: How to make a NSURLSesssion GET request with cookies
body: <p>I'm using the Pinterest SDK to download a Pinterest Pin's link, (sample link that I get back from the server:  <a href="https://www.pinterest.com/r/pin/186195765822871832/4801566892554728205/77314e40aeb26c0dc412e9cfa82f8dccc401fdb2b9806a3fe17ba8bafdb50510" rel="noreferrer">https://www.pinterest.com/r/pin/186195765822871832/4801566892554728205/77314e40aeb26c0dc412e9cfa82f8dccc401fdb2b9806a3fe17ba8bafdb50510</a>).</p>
  
  <p>About 5 days ago I started getting 404 errors in my NSURLSesssion when trying to access similar links that I'd pulled down from Pinterest.  </p>
  
  <p>A friend said that he believes Pinterest must now require cookies to access that link.  </p>
  
  <p>How can I configure my session so that I can use cookies and get a 200 response back from Pinterest? </p>
  
  <p>UPDATED CODE: </p>
  
  <pre><code>import UIKit
  import PlaygroundSupport
  
  
  var url = URL(string: "https://www.pinterest.com/r/pin/186195765822871832/4801566892554728205/77314e40aeb26c0dc412e9cfa82f8dccc401fdb2b9806a3fe17ba8bafdb50510")
  
  var getSourceURLFromPinterest: URLSessionDataTask? = nil
  let sessionConfig: URLSessionConfiguration = URLSessionConfiguration.default
  
  sessionConfig.timeoutIntervalForRequest = 30.0
  sessionConfig.timeoutIntervalForResource = 30.0
  
  let cookieJar = HTTPCookieStorage.shared
  let cookieHeaderField = ["Set-Cookie": "key=value, key2=value2"]
  let cookies = HTTPCookie.cookies(withResponseHeaderFields: cookieHeaderField, for: url!)
  HTTPCookieStorage.shared.setCookies(cookies, for: url, mainDocumentURL: url)
  
  let session = URLSession(configuration: sessionConfig)
  getSourceURLFromPinterest = session.dataTask(with: url! as URL) { (data: Data?, response: URLResponse?, error: Error?) -&gt; Void in
  
          if error != nil {
          print("error is \(error)")
      }
      if response == nil {
      print("no response")
  
      } else if let _ = data {
  
          //Config Request
          let request = NSMutableURLRequest(
              url: (response?.url)!,
              cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
              timeoutInterval: 30.0)
          request.httpMethod = "HEAD"
  
          var statusCode = Int()
          let session = URLSession.shared
          let checkURLForResponse = session.dataTask(with: request as URLRequest) {urlData, myResponse, responseError in
              if let httpResponse = myResponse as? HTTPURLResponse {
                   statusCode = httpResponse.statusCode
                                 switch statusCode {
                  case  _ where statusCode &lt; 500 &amp;&amp; statusCode &gt; 299 &amp;&amp; statusCode != 405: //whitelisted 405 to exclude Amazon.com false errors
                      print("status code \(statusCode) for \(url)")
                  default:
                      break;
                  }
              } else {  print("***NO httpResponse for \(url)") }
          }
          checkURLForResponse.resume()
      }
  }
   getSourceURLFromPinterest!.resume()
  
  PlaygroundPage.current.needsIndefiniteExecution = true
  </code></pre>