title: JSON Error when posting JSON
body: <p>I keep getting an error when trying to send JSON data to a site. But when I check the site, I see that all the data in json was sent and is correct. </p>
  
  <p>The error I get is: </p>
  
  <pre><code>Error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
  </code></pre>
  
  <p>The code I have is:</p>
  
  <pre><code>let json = [
              "name": "john",
              "last name": "smith"
          ]
  
          do{
  
              let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted)
  
  
              let url = NSURL(string: website)
  
              let request = NSMutableURLRequest(URL: url!)
  
              request.HTTPMethod = "POST"
  
              request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
              request.HTTPBody = jsonData
  
              let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
  
                  if error != nil{
                      print("Error: \(error)")
                      return
                  }
  
                  do {
                      let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]
  
                      print("Result: \(result)")
  
                  } catch {
                      print("Error: \(error)")
                  }
              }
  
              task.resume()
  
          } catch {
              print(error)
          }
  </code></pre>
  
  <p>When I allow for fragments:</p>
  
  <pre><code>try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
  </code></pre>
  
  <p>I get another error:</p>
  
  <pre><code>Error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
  </code></pre>
  
  <p>I changed:</p>
  
  <pre><code>try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
  </code></pre>
  
  <p>to </p>
  
  <pre><code>try NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.AllowFragments)
  </code></pre>
  
  <p>and I no longer get an error. </p>