title: IOS Swift Call Web Service using SOAP
body: <p>I have looked through the answers to how to call a web service via SOAP form swift on the internet and found some answers.  I have tried to implement the code I have found in those answers but continually get a http 400 status code.  I am trying to figure our what I am doing wrong.  </p>
  
  <p>I have distilled the problem down to a few lines of code in a view controller as seen below an the code is called when a button on the UI is pressed.  The web service I am trying to call can be found at <a href="http://www.cgsapi.com/CGSWebService.asmx" rel="noreferrer">http://www.cgsapi.com/CGSWebService.asmx</a>. </p>
  
  <p>(To view the WSDL file append ?wsdl to the end of the URL.)</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  var is_SoapMessage: String = "&lt;soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cgs=\"http://www.cgsapi.com/\"&gt;&lt;soapenv:Header/&gt;&lt;soapenv:Body&gt;&lt;cgs:GetSystemStatus/&gt;&lt;/soapenv:Body&gt;&lt;/soapenv:Envelope&gt;"
  
  override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  @IBAction func btnClicked(sender: AnyObject)
  {
      var is_URL: String = "http://www.cgsapi.com/CGSWebService.asmx"
  
      var lobj_Request = NSMutableURLRequest(URL: NSURL(string: is_URL)!)
      var session = NSURLSession.sharedSession()
      var err: NSError?
  
      lobj_Request.HTTPMethod = "POST"
      lobj_Request.addValue("www.cgsapi.com", forHTTPHeaderField: "Host")
      lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
      //lobj_Request.addValue(String(count(is_SoapMessage)), forHTTPHeaderField: "Content-Length")
      lobj_Request.addValue("223", forHTTPHeaderField: "Content-Length")
      lobj_Request.addValue("http://www.cgsapi.com/GetSystemStatus", forHTTPHeaderField: "SOAPAction")
  
      var task = session.dataTaskWithRequest(lobj_Request, completionHandler: {data, response, error -&gt; Void in
          println("Response: \(response)")
          var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
          println("Body: \(strData)")
  
          if error != nil
          {
              println("Error: " + error.description)
          }
  
      })
      task.resume()
  }
  
  }
  </code></pre>
  
  <p>Any idea why I am getting http 400 status when I call this?</p>