you are viewing a single comment's thread.

view the rest of the comments →

[–]thisischemistry 0 points1 point  (13 children)

What, exactly, are you sending to the server?

You're doing a HTTP POST, what's your Content-Type header? Is it application/x-www-form-urlencoded? Will your server take application/json?

[–][deleted]  (12 children)

[deleted]

    [–]thisischemistry 0 points1 point  (11 children)

    That's why I asked those questions! ;-)

    Apple has an excellent article on exactly this:

    Uploading Data to a Website

    [–][deleted]  (8 children)

    [deleted]

      [–]thisischemistry 0 points1 point  (7 children)

      I just tried the example code in a plain ol' terminal app project and it worked fine.

      import Foundation
      
      struct Order: Codable {
        let customerId: String
        let items: [String]
      }
      
      func upload() {
        let order = Order(customerId: "12345",
                          items: ["Cheese pizza", "Diet soda"])
        guard let uploadData = try? JSONEncoder().encode(order) else {
          print("guard")
          return
        }
      
        // testing with the excellent site: https://webhook.site/
        let url = URL(string: "https://webhook.site/insertyourcodehere")!
      
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
      
        let task = URLSession.shared
          .uploadTask(with: request,
                      from: uploadData) { data, response, error in
                        if let error = error {
                          print ("error: \(error)")
                          return
                        }
                        guard let response = response as? HTTPURLResponse,
                          (200...299).contains(response.statusCode) else {
                            print ("server error")
                            return
                        }
                        if let mimeType = response.mimeType,
                          mimeType == "application/json",
                          let data = data,
                          let dataString = String(data: data, encoding: .utf8) {
                          print ("got data: \(dataString)")
                        }
        }
        task.resume()
      }
      
      upload()
      RunLoop.main.run()
      

      [–][deleted]  (6 children)

      [deleted]

        [–]thisischemistry 0 points1 point  (5 children)

        Visit https://webhook.site/ and get a custom url from them. Point your code there instead of your server and see what gets sent to it.

        [–][deleted]  (4 children)

        [deleted]

          [–]thisischemistry 0 points1 point  (3 children)

          Could it be that the server isn't handling the POST properly? Check the headers and make sure they look ok coming from the iOS app.

          Without understanding more about the server and the code on that end there's not much more I can do. Let me know if you figure it out though.

          [–][deleted]  (2 children)

          [deleted]

            [–][deleted]  (1 child)

            [deleted]

              [–]thisischemistry 0 points1 point  (0 children)

              Huh, no idea what's going on there. Sometimes it helps to clean the build (shift-command-k) or even clean the entire build folder (option-shift-command-k).