Hi, I’m trying to upload an image to a database stored in a FileMaker Server with Swift, using FileMaker 17 API, but I can’t make it work, while in POSTMAN everything works.
This is the code I implemented:
func insertImage(completion: @escaping (Error?)->()) {
let image = UIImage(named: "adriana.jpg")
let data = image?.jpegData(compressionQuality: 1)! as! NSData
let boundary = generateBoundaryString()
let fullData = photoDataToFormData(data: data, boundary: boundary, fileName: "adriana.jpg")
let url = baseURL.appendingPathComponent("/layouts/LayContact/records/12/containers/picture/1")
print("URL \(url)")
var request = URLRequest(url: url)
request.httpMethod = "POST"
guard let tokenGuard = self.token else { return }
request.addValue("Bearer \(tokenGuard)", forHTTPHeaderField: "Authorization")
request.setValue("multipart/form-data; boundary=" + boundary,
forHTTPHeaderField: "Content-Type")
request.setValue(String(fullData.length), forHTTPHeaderField: "Content-Length")
request.httpBody = fullData as Data
request.httpShouldHandleCookies = false
let config = URLSessionConfiguration.default
let urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
urlSession.dataTask(with: request) { data, _, error in
guard let data = data, error == nil,
let json = try? JSONSerialization.jsonObject(with: data) as! [String: Any],
let _ = json["response"] as? [String: Any],
let messages = json["messages"] as? [[String: Any]],
let message = messages[0]["message"] as? String
else {
debugPrint("error!")
completion(error)
return
}
if message == "OK" {
completion(nil)
} else {
completion(error)
print(message)
}
}.resume()
}
private func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
func photoDataToFormData(data:NSData,boundary:String,fileName:String) -> NSData {
var fullData = NSMutableData()
// 1 - Boundary should start with --
let lineOne = "--" + boundary + "\r\n"
fullData.append(lineOne.data(
using: String.Encoding.utf8,
allowLossyConversion: false)!)
// 2
let lineTwo = "Content-Disposition: form-data; name=\"image\"; filename=\"" + fileName + "\"\r\n"
NSLog(lineTwo)
fullData.append(lineTwo.data(
using: String.Encoding.utf8,
allowLossyConversion: false)!)
// 3
let lineThree = "Content-Type: image/jpg\r\n\r\n"
fullData.append(lineThree.data(
using: String.Encoding.utf8,
allowLossyConversion: false)!)
// 4
fullData.append(data as Data)
// 5
let lineFive = "\r\n"
fullData.append(lineFive.data(
using: String.Encoding.utf8,
allowLossyConversion: false)!)
// 6 - The end. Notice -- at the start and at the end
let lineSix = "--" + boundary + "--\r\n"
fullData.append(lineSix.data(
using: String.Encoding.utf8,
allowLossyConversion: false)!)
return fullData
}
And I am getting the error:
Upload payload must contain a part named 'upload'.
Does anyone know how to fix this problem?
Thanks
I have a Swift app that uses the Data API, but I don't use it to upload images so I can't tell (at first glance) what might be wrong. If it's working in Postman, try selecting the "Code" button in the upper right corner.
That opens up a window with code snippets for several platforms, including Swift.