[英]Uploading an image with Alamofire not succeeding
我正在尝试向具有身体参数img(图像)和id(可以是任何数字)的端点发出POST请求。
这在Postman中有效(尽管除非缺少id
否则服务器永远不会以失败/成功响应):
但是,在iOS应用程序方面:
class func postPreviewImage(operaID: Int, image: UIImage, completion: @escaping () -> Void) {
let URL_CORDINATE = "http://artaugmentedreality.com/api/postpreview/"
print("Trying to post a preview of opera with ID:", operaID)
guard let imgData = image.jpegData(compressionQuality: 0.2) else {
print("Error while retrieving image data")
return
}
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imgData, withName: "img", fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpeg")
multipartFormData.append(operaID.data, withName: "id")
},
to: URL_CORDINATE,
method: .post)
{ (result) in
print("Result of Alamofire call is", result)
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (progress) in
print("Upload Progress: \(progress.fractionCompleted)")
})
case .failure(let encodingError):
print("Error while uploading image:", encodingError)
}
}
}
extension Int {
var data: Data {
var int = self
return Data(bytes: &int, count: MemoryLayout<Int>.size)
}
}
它正在上传,但是服务器不知何故没有收到它。 我的请求代码中可能有问题吗? 我得到:
Error copying matching creds. Error=-25300, query={
atyp = http;
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = http;
"r_Attributes" = 1;
sdmn = "artaugmentedreality.com";
srvr = "artaugmentedreality.com";
sync = syna;
}
来自Alamofire的错误代码,可能是因为该域不是HTTP。 但是,我将该域包括在info.plist的允许域列表中:
所以我对如何从这里出发一无所知。
您正在发送ID参数和图片。 因此,请尝试使用utf8编码。
multipartFormData.append(operaID.data(using: .utf8)!, withName: "id")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.