Sunday, November 27, 2022
HomeIOS Developmentios - Set boundary on URLSession.uploadTask :with :fromFile

ios – Set boundary on URLSession.uploadTask :with :fromFile

I’m making an attempt to add a picture to a spring backend. It is presupposed to work when within the background, subsequently I can solely use the session.uploadTask perform
My downside is that the backend is anticipating me to set a Content material-Kind header. One essential half is to outline the boundary and use it accordingly in my request physique, however how am I presupposed to set my boundary on an Picture?

Most tutorials I’ve seen do that with the session.uploadData perform, which is not out there once you wish to do the operation within the background. There I may merely append the boundary to the information.

To summarise: How can I exploit the header discipline boundary appropriately when importing pictures with uploadTask(with request: URLRequest, fromFile fileURL: URL)?

I get this error from spring:

org.springframework.internet.multipart.MultipartException: Present request isn't a multipart request

My Code:

let boundary = UUID().uuidString
// A background add job should specify a file
var imageURLRequest = URLRequest(url: uploadURL)
imageURLRequest.httpMethod = "Put up"
imageURLRequest.setValue("multipart/form-data; boundary=(boundary)", forHTTPHeaderField: "Content material-Kind")
let imageTask = URLBackgroundSession.shared.uploadTask(with: imageURLRequest, fromFile: URL(fileURLWithPath: imagePath))



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments