6

I am trying to send a file (png, doc, jpg, etc...) to my server. However for me to save the file properly I'd like to add the file extension to my POST request. Apple.developer says "The body stream and body data in this request object are ignored." Is there a way to add body data to my request? Here's what I have so far:

func uploadFile(filePath: String, endpoint: String) -> Void {

    let session = URLSession.shared
    let url = URL(string: endpoint)!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    let fileUrl = URL(fileURLWithPath: filePath)

    let task = session.uploadTask(with: request, fromFile: fileUrl)
    task.resume()

}

I tried to switch to using uploadTask(with:data:) but I am not sure how to add a file to data since most examples are using image

OmO Walker
  • 611
  • 6
  • 13
  • Does this answer your question? [Set boundary on URLSession.uploadTask :with :fromFile](https://stackoverflow.com/questions/74510888/set-boundary-on-urlsession-uploadtask-with-fromfile) – MJegorovas May 17 '23 at 08:18

0 Answers0