Alamofire 4 文件上传参数问题
Alamofire 4 File Upload Parameter issue
请看这里邮递员的照片
POSTMAN success result
但是代码没有 运行 我遗漏了一些东西请帮帮我
var parameters = [String: String]()
parameters = [
"profile_image": "imagefile"
]
let headers: HTTPHeaders = [
"Accept": "application/json"
]
let url = "http://******/public/api/v1/profile/update-picture?api_token=" + "aySlC26ZTHtlnS0lhUpdghxkd9gKJBLXLYFUO2Jidmiisoka9iFIicwRIZFx"
let completeUrl = URL(string:url)!
let imageData = UIImageJPEGRepresentation(chosenImage!, 1)
print ("image data:: \(imageData)")
print ("chosenImage:: \(chosenImage)")
// Alamofire.upload(imageData!, to: completeUrl).response { response in
// print (response)
// }
Alamofire.upload(
multipartFormData: {
multipartFormData in
multipartFormData.append(imageData!,
withName: "imagefile",
fileName: "image.jpg",
mimeType: "image/jpeg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, withName: key)
}
},
to: completeUrl,
headers: headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON{ response in
print(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
Postman Response
邮递员成功但 headers 有问题,你能指出我犯的愚蠢错误吗
我认为您需要以这种方式附加图片
multipartFormData.append(imageData!,
withName: "profile_image",
fileName: "image.jpg",
mimeType: "image/jpeg")
而且您不需要设置或包含 parameters = ["profile_image": "image file"]
如@rob 所述,使用 Charles 并将来自 Postman 的请求与来自模拟器的请求进行比较。这将有助于查明不同之处
请看这里邮递员的照片 POSTMAN success result 但是代码没有 运行 我遗漏了一些东西请帮帮我
var parameters = [String: String]()
parameters = [
"profile_image": "imagefile"
]
let headers: HTTPHeaders = [
"Accept": "application/json"
]
let url = "http://******/public/api/v1/profile/update-picture?api_token=" + "aySlC26ZTHtlnS0lhUpdghxkd9gKJBLXLYFUO2Jidmiisoka9iFIicwRIZFx"
let completeUrl = URL(string:url)!
let imageData = UIImageJPEGRepresentation(chosenImage!, 1)
print ("image data:: \(imageData)")
print ("chosenImage:: \(chosenImage)")
// Alamofire.upload(imageData!, to: completeUrl).response { response in
// print (response)
// }
Alamofire.upload(
multipartFormData: {
multipartFormData in
multipartFormData.append(imageData!,
withName: "imagefile",
fileName: "image.jpg",
mimeType: "image/jpeg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, withName: key)
}
},
to: completeUrl,
headers: headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON{ response in
print(response)
}
case .failure(let encodingError):
print(encodingError)
}
}
)
Postman Response 邮递员成功但 headers 有问题,你能指出我犯的愚蠢错误吗
我认为您需要以这种方式附加图片
multipartFormData.append(imageData!,
withName: "profile_image",
fileName: "image.jpg",
mimeType: "image/jpeg")
而且您不需要设置或包含 parameters = ["profile_image": "image file"]
如@rob 所述,使用 Charles 并将来自 Postman 的请求与来自模拟器的请求进行比较。这将有助于查明不同之处