I have an array of an object that has the following values:
var pictures = [Pictures]
struct Pictures: Codable, Hashable {
var picName1: String
var picName2: String
var picName3: String
var picFile1: UIImage
var picFile2: UIImage
var picFile3: UIImage
}
And I want to send it to my API through URLRequest.
I am encountering some issues with HTTP structuring.
I keep getting the response: "Failed to read the request form. Multipart body length limit 16384 exceeded."
This is my network/API call code:
func sendPicturesToServer() {
let url = URL(string: "https://myapicall.com/noodles")
let body = try! JSONEncoder().encode(pictures)
let boundary = "---------------------------14737809831466499882746641449"
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.httpBody = body
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
do {
let serializedResponse = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
print("Serialized Response: \(serializedResponse)")
} catch {
print(error)
}
}
task.resume()
}
}
Anyone have any suggestions?