title: Trouble converting JSON to data with Swift
body: <p>I am trying to learn Swift.  One of my projects is to try to retrieve JSON data from an internal web service (a group of Python CGI scripts) and convert it into a Swift object.  I can do this easily in Python, but I am having trouble doing this in Swift.  Here is my playground code:</p>
  
  <pre><code>import UIKit
  
  import XCPlayground
  
  XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
  
  let endpoint: String = "http://pathToCgiScript/cgiScript.py"
  let url = NSURL(string: endpoint)
  let urlrequest = NSMutableURLRequest(URL: url!)
  let headers: NSDictionary = ["User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)",
  "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"]
  urlrequest.allHTTPHeaderFields = headers as? [String : String]
  urlrequest.HTTPMethod = "POST"
  
  let config = NSURLSessionConfiguration.defaultSessionConfiguration()
  let session = NSURLSession(configuration: config)
  let task = session.dataTaskWithRequest(urlrequest) {
      (data, response, error) in
      guard data != nil else {
          print("Error: did not receive data")
          return
      }
      guard error == nil else {
          print("Error calling script!")
          print(error)
          return
      }
      do {
          guard let received = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as?
          [String: AnyObject] else {
              print("Could not get JSON from stream")
              return
          }
          print(received)
  
      } catch {
          print("error parsing response from POST")
      }
  }
  task.resume()
  </code></pre>
  
  <p>I know making a 'POST' to retrieve data may look odd, but that is how the system is set up.  I keep on getting:</p>
  
  <pre><code>Could not get data from JSON
  </code></pre>
  
  <p>I checked the response, and the status is 200.  I then checked the data's description with:</p>
  
  <pre><code>print(data?.description)
  </code></pre>
  
  <p>I got an unexpected result.  Here is a snippet:</p>
  
  <pre><code>Optional("&lt;0d0a5b7b 22535441 54555322 3a202244 6f6e6522 2c202242 55535922...
  </code></pre>
  
  <p>I used Mirror, and apparently the type is NSData.  Not sure what to make of this.  I have tried to encode the data with base64EncodedDataWithOptions.  I have tried different NSJSONReadingOptions as well to no avail.  Any ideas?</p>
  
  <p>Update:</p>
  
  <p>I used Wireshark to double check the code in the Playground.  Not only was the call made correctly, but the data being sent back is correct as well.  In fact, Wireshark sees the data as JSON.  The issue is trying to turn the JSON data into a Swift object.</p>