title: How Can I Trigger Two (2) HTTP POST REQUESTS Using URLSession (Swift 4.2)?
body: <p>I am creating a basic iOS mobile app to capture an image on my camera device by connecting directly via WiFI and using HTTP POST REQUEST. </p>
  
  <p>To capture an image, the function must trigger two (2) commands. One (1) to activate the shutter and one (1) to release the shutter. So this specific command needs to be executed 2x sequentially.</p>
  
  <p>This is the format of the POST REQUEST to the camera:</p>
  
  <pre><code>REQUEST TO TAKE A PICTURE
  POST
  camera_API_URL
  {
  "action": "half_press",
  "af": “true”
  }
  
  REQUEST TO STOP TAKING A PICTURE
  POST
  camera_API_URL
  {
  "action": "release",
  "af": “false”
  }
  </code></pre>
  
  <p>I first check to see if I can retrieve information about the device:</p>
  
  <pre><code>    guard let url = URL(string: "camera_API_URL") else { return }
  
          let session = URLSession.shared
          session.dataTask(with: url) { (data, response, error) in
          if let response = response {
              print(response)
          }
  
          if let data = data {
              print(data)
              do {
                  let json = try JSONSerialization.jsonObject(with: data,   options: [])
                  print(json)
              } catch {
                  print(error)
              }
          }
         }.resume()
  </code></pre>
  
  <p>This yields the following JSON data response:</p>
  
  <pre><code>&lt;NSHTTPURLResponse: 0x283bb96c0&gt; { URL: camera_API_URL } { Status Code: 200,   Headers {
  "Content-Length" =     (
      199
  );
  "Content-Type" =     (
      "application/json"
  );
  } }
  199 bytes
  {
  firmwareversion = "1.0.1";
  guid = 1q2w3e4r5t6y7u8i9o0p;
  macaddress = "04:1f:2e:12:1d:93";
  munufacturer = "Some Company";
  productname = "DSLR Camera Name";
  serialnumber = 1234567890;
  }
  </code></pre>
  
  <p>I then proceed to do the first trigger:</p>
  
  <pre><code>    let parametersOpenShutter = ["action": "half_press", "af": "true"]
      let parametersCloseShutter = ["action": "release", "af": "false"]
  
      guard let url = URL(string: "camera_API_URL") else { return }
  
      var request = URLRequest(url: url)
      request.httpMethod = "POST"
      request.addValue("application/json", forHTTPHeaderField: "Content-Type")
      guard let httpBody = try? JSONSerialization.data(withJSONObject: parametersOpenShutter, options: []) else { return }
      request.httpBody = httpBody
  
      let session = URLSession.shared
      session.dataTask(with: request) { (data, response, error) in
          if let response = response {
              print(response)
          }
  
          if let data = data {
              do {
                  let json = try JSONSerialization.jsonObject(with: data, options: [])
                  print(json)
              } catch {
                  print(error)
              }
          }
  
          }.resume()
  </code></pre>
  
  <p>This yields the following JSON response:</p>
  
  <pre><code>&lt;NSHTTPURLResponse: 0x2825bfa80&gt; { URL: camera_API_URL } { Status Code: 400,   Headers {
  "Content-Length" =     (
      31
  );
  "Content-Type" =     (
      "application/json"
  );
  } }
  {
  message = "Invalid parameter";
  }
  </code></pre>
  
  <p>I am unsure what I am doing wrong to get an invalid parameter.  </p>
  
  <p>Please check.  </p>
  
  <p>How should I code the app to perform (2) HTTP POST REQUESTS sequentially to open and close the shutter?</p>