我可以毫无问题地发送纯文本。
例如,我有此请求:
func buyWholeSaler(url : String, token:String, manager:String, fio:String,phone:String, sender:String, carrier:String, document:String,shipping_id:String,pay_type:String, shipping_pay:String, amount_nalogi:String, shipping_return:String,money_recipient :String,comment:String, completion: @escaping (JSON)-> Void) {
let headers: HTTPHeaders = [
"Authorization": "Bearer \(token)",
"Accept": "application/json"
]
let parameters: Parameters = [
"shipping_id" : shipping_id,
"pay_type": pay_type,
"manager" : manager,
"fio" : fio,
"phone" : phone,
"sender" : sender,
"carrier" : carrier,
"document" : document,
"shipping_pay": shipping_pay,
"amount_nalogi": amount_nalogi,
"shipping_return" :shipping_return,
"money_recipient" : money_recipient,
"comment" : comment
]
AF.request(url,method: .post, parameters: parameters, headers:headers).responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
completion(json)
case .failure(let error):
print(error)
}
}
}
但是如何发送带有这些参数的图片?
我有2张图片
var image1 = UIImage()
var image2 = UIImage()
如何重写请求以正确发送请求?例如:
let parameters: Parameters = [
"shipping_id" : shipping_id,
"pay_type": pay_type,
...
"ur_file_pdv": image1,
"ur_file_er": image1
]
我找到了此请求,但我不明白它是如何工作的:
AF.upload(multipartFormData: { multiPart in
for p in parameters {
multiPart.append("\(p.value)".data(using: String.Encoding.utf8)!, withName: p.key)
}
multiPart.append(imageVityagPdv.jpegData(compressionQuality: 0.4)!, withName: "ur_file_pdv", fileName: "ur_file_pdv.jpg", mimeType: "image/jpg")
multiPart.append(imageVityag.jpegData(compressionQuality: 0.4)!, withName: "ur_file_er", fileName: "ur_file_er.jpg", mimeType: "image/jpg")
}, to: url, method: .post, headers: headers) .uploadProgress(queue: .main, closure: { progress in
print("Upload Progress: \(progress.fractionCompleted)")
}).responseJSON(completionHandler: { data in
print("upload finished: \(data)")
let success = json["success"].stringValue
}).response { (response) in
switch response.result {
case .success(let resut):
print("upload success result: \(resut!)")
case .failure(let err):
print("upload err: \(err)")
}
}
似乎正在向服务器上载一些东西,但是命令没有发生
答案 0 :(得分:0)
您无法添加“数据”作为参数,尤其是因为进行了“请求”以从服务器检索数据,并且该请求非常小,仅向服务器提供了有关您实际希望从中接收的信息(以文字形式)。要上传内容,请使用POST请求,您可以在Alamofire中使用AF.upload进行请求,如下所示。
“不发生订单”是什么意思?也许您有一些服务器代码,以便我们可以了解问题所在?可能是因为它希望参数为GET,在这种情况下,您可以将其直接添加到URL。
func buyWholeSaler(url : String, token: String, manager: String,
fio: String, phone: String, sender: String, carrier: String,
document: String, shipping_id: String, pay_type: String,
shipping_pay: String, amount_nalogi: String,
shipping_return: String, money_recipient: String, comment:String,
completion: @escaping (JSON) -> Void) {
let headers: HTTPHeaders = [
"Authorization": "Bearer \(token)",
"Accept": "application/json"
]
let parameters: Parameters = [
"shipping_id" : shipping_id,
"pay_type": pay_type,
"manager" : manager,
"fio" : fio,
"phone" : phone,
"sender" : sender,
"carrier" : carrier,
"document" : document,
"shipping_pay": shipping_pay,
"amount_nalogi": amount_nalogi,
"shipping_return" :shipping_return,
"money_recipient" : money_recipient,
"comment" : comment
]
AF.upload(multipartFormData: { (multipartFormData) in
parameters.forEach({ multipartFormData.append("\(p.value)".data(using: .utf8)!, withName: p.key) })
multipartFormData.append(image1.jpegData(compressionQuality: 0.4)!,
withName: "ur_file_pdv", fileName: "ur_file_pdv.jpg", mimeType: "image/jpg")
multipartFormData.append(image2.jpegData(compressionQuality: 0.4)!,
withName: "ur_file_er", fileName: "ur_file_er.jpg", mimeType: "image/jpg")
},
to: url, method: .post, headers : headers).responseJSON { (response) in
switch response.result {
case .success(let value): completion(JSON(value))
case .failure(let error): print(error)
}
}
}
答案 1 :(得分:0)
self.uploadMultipart(buildedWith: { (multipartData) in
multipartData.append(assestPath, withName: "file", fileName: "mp4", mimeType: "")
假定路径将是图像url,应该是本地路径,或者也可以转换为数据形式,但这最好是直接附加路径。