title: How to create a json 'POST' request in SwiftUI?
body: <p>I'm making a contact list app for a project to learn programming, and am currently creating the method to add a contact to a database through a Rest API I created.</p>
  <p>The code below is for the <code>Add Contact</code> view, and I followed some tutorials on how to take data from a form, convert it to JSON and send it to a database via a POST request.</p>
  <p>This is the code I've gotten, and what should be happening is that I should see a print of <code>responseJSON</code>, but that isn't happening, in fact nothing is happening and when I run the <code>checkDetails</code> function by pressing the button on the view I get the prints of <code>body</code> and <code>jsonData</code>, but nothing else. Not even an error.</p>
  <p>I am a bit stumped here as I haven't changed much, if anything, from the tutorials I followed, so I am guessing it is something to do with my own code.</p>
  <p>Any help would be appreciated!</p>
  <pre><code>import SwiftUI
  import Combine
  
  struct AddContact: View {
      @State var id = 999
      
      @State var first_name: String = &quot;&quot;
      @State var last_name: String = &quot;&quot;
      @State var phone_number: String = &quot;&quot;
      @State var address: String = &quot;&quot;
      
      @State var birthday = Date()
      @State var birthdayString: String = &quot;&quot;
      @State var create_date = Date()
      @State var create_dateString: String = &quot;&quot;
      @State var updated_date = Date()
      @State var updated_dateString: String = &quot;&quot;
  
      @State var manager = DataPost()
      
      var body: some View {
          if manager.formCompleted {
              Text(&quot;Done&quot;).font(.headline)
          }
          VStack {
              NavigationView {
                  Form {
                      Section() {
                          TextField(&quot;First Name&quot;, text: $first_name)
                          TextField(&quot;Last Name&quot;, text: $last_name)
                      }
                      Section() {
                          TextField(&quot;Phone Number&quot;, text: $phone_number)
                          TextField(&quot;Address&quot;, text: $address)
                      }
                      Section() {
                          DatePicker(&quot;Birthday&quot;, selection: $birthday, displayedComponents: .date)
                      }
                      Section() {
                          Button(action: {
                              let dateFormatter = DateFormatter()
                              dateFormatter.dateStyle = .short
                              
                              birthdayString = dateFormatter.string(from: birthday)
                              create_dateString = dateFormatter.string(from: create_date)
                              updated_dateString = dateFormatter.string(from: updated_date)
                              
                              print(&quot;Clicked&quot;)
                              
                              self.manager.checkDetails(id: self.id, first_name: self.first_name, last_name: self.last_name, phone_number: self.phone_number, address: self.address, birthday: self.birthdayString, create_date: self.create_dateString, updated_date: self.updated_dateString)
                              
                          }, label: {
                              Text(&quot;Add Contact&quot;)
                                  .fontWeight(.bold)
                                  .multilineTextAlignment(.center)
                          })
                      }.disabled(first_name.isEmpty || last_name.isEmpty || phone_number.isEmpty || address.isEmpty)
                  }
              }.navigationTitle(&quot;New Contact&quot;)
              .navigationBarTitleDisplayMode(.inline)
          }
      }
  }
  
  class DataPost: ObservableObject {
      var didChange = PassthroughSubject&lt;DataPost, Never&gt;()
      var formCompleted = false {
          didSet {
              didChange.send(self)
          }
      }
      
      func checkDetails(id: Int, first_name: String, last_name: String, phone_number: String, address: String, birthday: String, create_date: String, updated_date: String) {
          
          let body: [String: Any] = [&quot;data&quot;: [&quot;id&quot;: id, &quot;first_name&quot;: first_name, &quot;last_name&quot;: last_name, &quot;birthday&quot;: birthday, &quot;phone_number&quot;: phone_number, &quot;create_date&quot;: create_date, &quot;updated_date&quot;: updated_date, &quot;address&quot;: address]]
                  
          let jsonData = try? JSONSerialization.data(withJSONObject: body)
                  
          let url = URL(string: &quot;https://flaskcontact-list-app.herokuapp.com/contacts&quot;)!
          var request = URLRequest(url: url)
          
          request.httpMethod = &quot;POST&quot;
          
          request.setValue(&quot;\(String(describing: jsonData?.count))&quot;, forHTTPHeaderField: &quot;Content-Length&quot;)
          request.setValue(&quot;application/json&quot;, forHTTPHeaderField: &quot;Content-Type&quot;)
          
          request.httpBody = jsonData
  
          print(body)
          print(jsonData)
          
          let task = URLSession.shared.dataTask(with: request) { data, response, error in
              guard let data = data, error == nil else {
                  print(error?.localizedDescription ?? &quot;No data&quot;)
                  return
              }
  
              let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
              if let responseJSON = responseJSON as? [String: Any] {
                  print(responseJSON)
              }
          }
  
          task.resume()
      }
  }
  </code></pre>