我有一个对象的数组,它具有以下值:
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
}
我想通过URLRequest将其发送到我的API。
我遇到了一些HTTP结构问题。
我不断得到回应:"Failed to read the request form. Multipart body length limit 16384 exceeded."
这是我的网络/API调用代码:
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()
}
}
有人有什么建议吗?
1条答案
按热度按时间nue99wik1#