title: Swift HTTP POST request with Combine
body: <p>In my app, I am using the Combine framework to make network requests and it works fine for GET requests. But I am running into this issue with POST requests.</p>
  <p>The following code (without Combine) works fine:</p>
  <pre><code>let data = [&quot;statusTime&quot;: DateFormatter.iso8601Full.string(from: Date())]
  let requestBody = try? JSONSerialization.data(withJSONObject: data, options: [])
  let baseURL = &quot;my-api.amazonaws.com&quot;
  let endpoint = &quot;/my/endpoint&quot;
  
  var urlComponents = URLComponents()
  urlComponents.scheme = &quot;https&quot;
  urlComponents.host = baseURL
  urlComponents.path = endpoint
  let url = urlComponents.url!
  var request = URLRequest(url: url)
  
  request.httpMethod = &quot;POST&quot;
  request.httpBody = requestBody
  
  request.addValue(authorizationToken, forHTTPHeaderField: &quot;Authorization&quot;)
  request.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Content-Type&quot;)
  request.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Accept&quot;)
  
  let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
      if let error = error {
          print(&quot;error: \(error)&quot;)
          return
      }
      print(&quot;Response: \((response as! HTTPURLResponse).statusCode)&quot;)
  }
  task.resume()
  </code></pre>
  <p>I get a 200 response code from the above code.</p>
  <p>The same code with Combine:</p>
  <pre><code>let data = [&quot;statusTime&quot;: DateFormatter.iso8601Full.string(from: Date())]
  let requestBody = try? JSONSerialization.data(withJSONObject: data, options: [])
  let baseURL = &quot;my-api.amazonaws.com&quot;
  let endpoint = &quot;/my/endpoint&quot;
  
  var urlComponents = URLComponents()
  urlComponents.scheme = &quot;https&quot;
  urlComponents.host = baseURL
  urlComponents.path = endpoint
  let url = urlComponents.url!
  var request = URLRequest(url: url)
  
  request.httpMethod = &quot;POST&quot;
  request.httpBody = requestBody
  
  request.addValue(authorizationToken, forHTTPHeaderField: &quot;Authorization&quot;)
  request.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Content-Type&quot;)
  request.addValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Accept&quot;)
  
  let cancellable = URLSession.shared
      .dataTaskPublisher(for: request)
      .print()
      .sink(receiveCompletion: {
          if let error = $0.error {
              print(&quot;Failure: \(error)&quot;)
          }
      }, receiveValue: {
          print(&quot;\($0)&quot;)
      })
  </code></pre>
  <p>Here is the output I receive:</p>
  <pre><code>receive subscription: (DataTaskPublisher)
  request unlimited
  receive cancel
  </code></pre>
  <p>Seems like the request with the Combine framework is somehow getting cancelled? I used a network tracing app and I can see the first request (without Combine), but not the second request.</p>
  <p>I have looked at numerous posts and documentation, but can't see what's wrong with my code. What am I missing? Thanks in advance.</p>