title: Status code 400 while making POST request in swift
body: <p>I keep getting this error which I don't know how to resolve! 
  I'm trying to parse access token from a web API and for that I've set up post request. 
  Somehow it's throwing an error on 'responseString' line. </p>
  
  <pre><code>import UIKit
  import Foundation
  
  class ViewController: UIViewController, NSURLConnectionDelegate {
  
      @IBOutlet var userName: UITextField!
      @IBOutlet var password: UITextField!
  
      @IBAction func submit(sender: AnyObject) {
  
          let postString = "username=" + userName.text + "&amp;password=" + password.text + "&amp;grant_type=password";
          let data: NSData = postString.dataUsingEncoding(NSUTF8StringEncoding)!
          let base64LoginString = data.base64EncodedStringWithOptions(nil)
          let postLength=NSString(format: "%ld", data.length)
  
          let url = NSURL(string: "http://www.myurl.com/Token")
          let request = NSMutableURLRequest(URL: url!);
          request.HTTPMethod = "POST"
          request.HTTPBody = data
          request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
          request.addValue(base64LoginString, forHTTPHeaderField: "Authorization")
          request.addValue("application/json", forHTTPHeaderField: "Accept")
          request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
  
          let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {data, response, error in
  
              if error != nil {
              println("error = \(error)")
              return
  
              }
          //print out response object
              println("******* response = \(response)")
  
          //print out response body // THIS LINE THROWS STATUS CODE 400 ERROR
              let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
              println("******* response data = \(responseString)")
  
              var err: NSError?
              var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &amp;err) as? NSDictionary
  
              if let parseJSON = json {
                  var token = parseJSON["access_token"] as? String
                  println("token: \(token)")
  
              }
  
          }
  
  
          task.resume()
  
      }
  </code></pre>
  
  <p>This is the error</p>
  
  <pre><code>******* response = &lt;NSHTTPURLResponse: 0x7ff1eb4496d0&gt; { URL: https://www.myurl.com/Token } { status code: 400, headers {
      "Cache-Control" = "no-cache";
      "Content-Length" = 34;
      "Content-Type" = "application/json;charset=UTF-8";
      Date = "Mon, 10 Aug 2015 20:16:14 GMT";
      Expires = "-1";
      Pragma = "no-cache";
      "X-Powered-By" = "ASP.NET";
  } }
  </code></pre>