Swift Multipart Upload Not Sending Form Data

Viewed 12

I'm trying to follow the second answer to this question:

upload an image on a server Swift 4

...because I want to both upload an image, as well as the photographer, tags, and description.

So in the example in the answer he has:

 let parameters = [
       [
         "key": "myFile",
         "src": imageData,
         "type": "image/png"
       ],
       [
         "key": "key",
         "value": "7d67b170-199c-4436-ad9d-cd5daf38f905",
         "type": "text"
       ]] as [[String : Any]]

Which I've sort of drawn out to be (I'll post the full code at the bottom):

                let parameters = [
                       [
                         "key": "file",
                         "src": imageData as Any,
                         "type": "image/png"
                       ],
                       [
                         "key": "tags",
                        // "value": tags2,
                         "value": "TR",
                         "type": "text"
                       ],
                       [
                         "key": "name",
                        // "value": uname,
                         "value": "TR",
                         "type": "text"
                       ],
                       [
                         "key": "fname",
                         //"value": remoteName,
                         "value": "TR",
                         "type": "text"
                       ],
                       [
                         "key": "desc",
                         // "value": desc2,
                         "value": "TR",
                         "type": "text"
                       ],
                       [
                         "key": "title",
                         //"value": title2,
                         "value": "TR",
                         "type": "text"
                       ]
                ] as [[String : Any]]

The problem is that NONE of these values is getting picked-up by the server. On Server response... for example... "name" is returning "null."

Here's the complete code:

            let formatter = DateFormatter()
                            formatter.dateFormat = "HHmmssddMMyyyy"
                         let stringOfDateTimeStamp = formatter.string(from: Date())
            let remoteName = "IMG\(stringOfDateTimeStamp)"+".jpg"
                    let parameters = [
                           [
                             "key": "file",
                             "src": imageData as Any,
                             "type": "image/png"
                           ],
                           [
                             "key": "tags",
                            // "value": tags2,
                             "value": "TR",
                             "type": "text"
                           ],
                           [
                             "key": "name",
                            // "value": uname,
                             "value": "TR",
                             "type": "text"
                           ],
                           [
                             "key": "fname",
                             //"value": remoteName,
                             "value": "TR",
                             "type": "text"
                           ],
                           [
                             "key": "desc",
                             // "value": desc2,
                             "value": "TR",
                             "type": "text"
                           ],
                           [
                             "key": "title",
                             //"value": title2,
                             "value": "TR",
                             "type": "text"
                           ]
                    ] as [[String : Any]]
            
            
            let boundary = "Boundary-\(UUID().uuidString)"

for param in parameters {
           let paramName = param["key"]!
            body.append("--\(boundary)\r\n".data(
                using: String.Encoding.utf8,
            allowLossyConversion: false)!)

        body.append("Content-Disposition:form-data; name=\"\(paramName)\"".data(
            using: String.Encoding.utf8,
        allowLossyConversion: false)!)
  
          let paramValue = param["value"] as! String

          body.append("\r\n\r\n\(paramValue)\r\n".data(
              using: String.Encoding.utf8,
          allowLossyConversion: false)!)
        
    }

            body.append("--\(boundary)--\r\n".data(
                              using: String.Encoding.utf8,
                          allowLossyConversion: false)!)

            guard let url: URL = URL(string: "https://example.com/ap/upload.php?uid=" + uid + "&uploaded_file=" + remoteName + "&fname=" + remoteName) else {
                print("Invalid URL")
                return
            }
            var urlRequest = URLRequest(url: url)
            urlRequest.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
            urlRequest.setValue(String(body.count), forHTTPHeaderField: "Content-Length")
            urlRequest.httpBody = body
            urlRequest.httpMethod = "POST"
            
            URLSession.shared.dataTask(with: urlRequest, completionHandler: {
                (data, response, error) in
                guard let data = data else {
                    print("Error in Data")
                    return
                }
                
                let responseStr: String = String(data: data, encoding: .utf8) ?? ""
                print(responseStr)

            })
            
                .resume()
            
        }, label: {
            Text("Upload Image")
        })
        
       
    }
    .sheet(isPresented: $showImagePicker, content: {
        ImagePicker(image: self.$selectedImage)
    })
}

}

I know that it at least REACHES the server... because there's a server response... but none of the "parameters" return anything but null. I'm sure it's something simple like I've missed a Bracket or something...

0 Answers
Related