【发布时间】:2018-01-20 21:57:42
【问题描述】:
我需要使用图像文件和 JSON 调用 Multipart 请求。
我试过了,还是报错。
// define parameters
let parameters = [
"hometown": "yalikavak",
"living": "istanbul"
]
Alamofire.upload(multipartFormData: { multipartFormData in
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.append(imageData, withName: "file", fileName: "file.png", mimeType: "image/png")
}
for (key, value) in parameters {
multipartFormData.append((value?.data(using: .utf8))!, withName: key)
}}, to: "upload_url", method: .post, headers: ["Authorization": "auth_token"],
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.response { [weak self] response in
guard let strongSelf = self else {
return
}
debugPrint(response)
}
case .failure(let encodingError):
print("error:\(encodingError)")
}
})
}
如何发送 JSON?
【问题讨论】:
-
你在这个 sn-p 上有什么错误吗?
-
是的,我收到带有消息的 HTTP CODE 400 - 错误请求。
-
我可以有请求网址吗?所以我可以尝试以更好的方式检查它
-
对不起!出于安全考虑,我不允许共享请求 URL。
-
我猜这个答案与您的问题有关。 stackoverflow.com/questions/41875919/…
标签: ios swift alamofire multipart alamofireimage