title: How can I make a POST Request in Swift with parameters using URLSession
body: <p>I have a post request that I want to make using URLSession.</p>
  <p>The post request looks like this:</p>
  <pre><code>curl -X POST   'https://smartdevicemanagement.googleapis.com/v1/enterprises/privatekey/devices/devicekey:executeCommand'   -H 'Content-Type: application/json'   -H 'Authorization: authtoken'   --data-raw '{
  &quot;command&quot; : &quot;sdm.devices.commands&quot;,
  &quot;params&quot; : {
    &quot;commandName&quot; : &quot;cmdValue&quot;
   }
  }'
  </code></pre>
  <p>As this is a POST request, I want to only decode if the response is an error message.</p>
  <p>Here is the code I currently have:</p>
  <pre><code>guard let url = URL(string: &quot;https://smartdevicemanagement.googleapis.com/v1/enterprises/\(project_id)/devices&quot;) else {return}
      
  var request = URLRequest(url: url)
  request.httpMethod = &quot;POST&quot;
  request.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Content-Type&quot;)
  request.addValue(&quot;token&quot;, forHTTPHeaderField: &quot;Authorization&quot;)
  let cmdParams: [String: String] =  [&quot;command&quot;:&quot;sdm.devices.commands&quot;, &quot;params&quot; : [&quot;commandName&quot;: &quot;cmdValue&quot;]]
  
  do {
      request.httpBody = try JSONSerialization.data(withJSONObject: cmdParams)
  } catch let error {
      print(error.localizedDescription)
      }
  
  URLSession.shared.dataTask(with: request) { (data, response, error) in
      guard error == nil else {print(error!.localizedDescription); return }
      guard let data = data else {print(&quot;empty data&quot;); return }
  </code></pre>
  <p>The cmdParams are throwing an error, so I'm not sure how to structure the params request properly, a successful POST will result in the API returning <code>{}</code> an unsuccessful request will return some error.</p>
  <p>How can I adjust my code to get this working?</p>