I want to upload image using Multipart/formdata put request in iOS Swift using Almofire Networking which has few parameters and one parameter contains JSON. eg
"parameter_name":{
"field_1" : {
"type" : "MULTISELECT",
"value" : [
"Option 1",
"Option 3"
],
"name" : "Multi Selection"
},
"field_2" : {
"type" : "DATE",
"name" : "BirthDate",
"value" : "2000-09-08"
},
"field_3" : {
"type" : "SINGLESELECT",
"value" : "Option1",
"name" : "Single Select"
}
}
This is one parameter in multipart formdata with other parameters like name, email, photo and more (all are of type string) Could anyone help me achieving this as using Swift & AlamoFire As I am receiving 503 status code for this. Note:- In postman same API is working fine.
What I have done so far is:
Alamofire.upload(multipartFormData: { multipartData in
parameters.forEach { (key, value) in
if let arrdata = (value as? [AnyHashable : Any]) {
if arrdata.count > 0 {
let arrdatas = try! JSONSerialization.data(withJSONObject: arrdata, options: [])
multipartData.append(arrdatas, withName: key as String)
}
}
guard
let data = (value as? String)?
.data(using: .utf8)
else { return }
multipartData.append(data, withName: key)
}
if let img = photo,
let data = UIImageJPEGRepresentation(img, 0.0) {
multipartData
.append(data,
withName: Keys.photo,
fileName: Keys.fileName,
mimeType: Keys.mimeType)
}
},
to: "My URL",
headers: requestType.headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseData(completionHandler: { response in
if let statusCode = response.response?.statusCode,
200 <= statusCode && statusCode < 300,
let data = response.data {
guard
let decoded = try? JSONSerialization.jsonObject(with: data, options: []),
let dic = decoded as? [String: Any] else {
return
}
printOnlyOnDebug("Response got is --> \(self.convertToJSON(toConvert: dic))")
success(data)
} else {
guard
let data = response.data,
let decoded = try? JSONSerialization.jsonObject(with: data, options: []),
let dic = decoded as? [String: Any],
let message = dic[Keys.message] as? String else {
let code = response.response?.statusCode == 503 ? 401 : response.response?.statusCode
failure(code, "Network error")
return
}
failure(response.response?.statusCode, message)
}
})
case .failure(let error):
failure(nil, error.localizedDescription)
}
})
Also header contains 'Content-Type' as 'multipart/form-data'
I forgot to mention method type PUT in AlamoFire request so it was creating POST request and it was causing the issue.
Alamofire.upload(multipartFormData: { multipartData in
parameters.forEach { (key, value) in
if let arrdata = (value as? [AnyHashable : Any]) {
if arrdata.count > 0 {
let arrdatas = try! JSONSerialization.data(withJSONObject: arrdata, options: [])
multipartData.append(arrdatas, withName: key as String)
}
}
guard
let data = (value as? String)?
.data(using: .utf8)
else { return }
multipartData.append(data, withName: key)
}
if let img = photo,
let data = UIImageJPEGRepresentation(img, 0.0) {
multipartData
.append(data,
withName: Keys.photo,
fileName: Keys.fileName,
mimeType: Keys.mimeType)
}
},
to: "My URL",
method:.put,
headers: requestType.headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseData(completionHandler: { response in
if let statusCode = response.response?.statusCode,
200 <= statusCode && statusCode < 300,
let data = response.data {
guard
let decoded = try? JSONSerialization.jsonObject(with: data, options: []),
let dic = decoded as? [String: Any] else {
return
}
printOnlyOnDebug("Response got is --> \(self.convertToJSON(toConvert: dic))")
success(data)
} else {
guard
let data = response.data,
let decoded = try? JSONSerialization.jsonObject(with: data, options: []),
let dic = decoded as? [String: Any],
let message = dic[Keys.message] as? String else {
let code = response.response?.statusCode == 503 ? 401 : response.response?.statusCode
failure(code, "Network error")
return
}
failure(response.response?.statusCode, message)
}
})
case .failure(let error):
failure(nil, error.localizedDescription)
}
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.