Alamofire photo upload with key of the file in body - swift

I am able to upload a photo via Postman but in my iOS application it fails, and it is so weird that I still get a .success from encodingCompletion.
Here is part of my code
UpdateUserInfo
//
// Update user info
//
func updateUserInfo(){
var imageData: Data!
var url = ""
if let userId = KeychainWrapper.standard.string(forKey: USER_ID_KEY){
url = URL_USER_UPLOAD_PIC + userId
}
if pickedImage != nil{
imageData = UIImagePNGRepresentation(pickedImage)
//imageData = UIImageJPEGRepresentation(pickedImage!, 1.0)
}
let token = KeychainWrapper.standard.string(forKey: USER_LOGIN_TOKEN_KEY)!
let headers: HTTPHeaders = [
"Authorization": "Bearer \(token)"
]
if imageData != nil{
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(imageData!, withName: "fileset", fileName: "file.png", mimeType: "image/png")
}, to: url,
method: .post,
headers: headers,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
print("Donkey Success \(String(describing: upload.response?.statusCode))")
upload.responseString(completionHandler: { (response) in
debugPrint(response)
})
case .failure(let encodingError):
print(encodingError)
print("Donkey Fail")
}
})
}
}
and in my postman I have
Postman
My first question is why I am getting a .success if it fails to upload?
and my second question is, do I need to put the key "pic" (seen in Postman) somewhere in my request? if so where?
Thanks for any help in advance

In Post man Image Key is pic but in your code its "fileset"
change to
multipartFormData.append(imageData!, withName: "pic", fileName: "file.png", mimeType: "image/png")
And success is Result of encoding not uploading processs
EncodingResult is MultipartFormDataEncodingResult that Defines
whether the MultipartFormData encoding was successful and contains
result of the encoding as associated values. - Success: Represents a successful MultipartFormData encoding and contains the
new UploadRequest along with
streaming information. - Failure: Used to represent a failure in the MultipartFormData encoding and also
contains the encoding

Related

using Alamofire 5 to upload file to RocketChat server

I'm using Alamofire 5, and I'm trying to upload an image to a Rocket Chat server. The corresponding curl statement that I need to duplicate using AF is at the following link:
(link to documentation: https://docs.rocket.chat/api/rest-api/methods/rooms/upload)
I've been trying to upload using multipartFormData with no success. I've also attempted to bypass Alamofire altogether and use Swift URLSession. The best I can do is get the same error message from the server, which is "errorType": invalid-field."
My code as it stands right now:
let url = URL_MESSAGES + "rooms.upload/\(_group._id ?? "")"
let boundary = UUID().uuidString
let headers: HTTPHeaders = [
"X-Auth-Token": authToken,
"X-User-Id": me._id ?? "",
"Content-type":"multipart/form-data; boundary=\(boundary)"
]
if let data = image.jpeg(.medium) {
print(data.count)
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(data, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
},
to: url, method: .post , headers: header)
.response { resp in
print(resp)
}
.cURLDescription { description in
print(description)
}
.responseString { [weak self] (response) in
DispatchQueue.main.async {
if response.error == nil {
guard let data = response.data else {
return completion(true,[:])
}
if let json = try? JSON(data: data) {
let dictionaryIn = json.rawValue as! [String : Any]
if (self?.isSuccess(data: dictionaryIn))! {
completion(true,json.rawValue as! [String : Any])
}else{
completion(false,[:])
self?.handleError(data: dictionaryIn)
}
}
}else{
completion(false,[:])
self?.handleError(data: [:])
}
}
}
}
}
I think you're breaking the upload by trying to set your own boundary. Alamofire will that for you automatically. Try removing the header.
The above code works perfectly, when I changed this:
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(data, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
to:
AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(data, withName: "file", fileName: "image.jpeg", mimeType: "image/jpeg")
Swapping out "image" for "file" fixed everything. I went with "image" because this is what every Alamofire tutorial I could find said to do. However, RocketChat requires it to be "file". It is in the documentation, but I didn't realize that's what it was telling me to do. Specifically, in the documentation, it says:
"Note: For some file types if uploading via curl you may need to set the mime type.
With some file types, curl will upload the file as application/octet-stream. You can pass a custom mime type like this: -F "file=#file.wav;type=audio/wav" "
I was trying -F "image=#file.wav;type=audio/wav" when translated from Alamofire. It needs to be: -F "file=#file.wav;type=audio/wav"

Multipart form data upload with Alamofire shows file missing in server

I'm trying to upload an image using Alamofire, the response shows success but the picture doesn't get uploaded. When I debugged with backend developer, it seemed the file attachment is missing in the request. However, the progress shows uploading details of the file. Can anyone help what's going wrong here.
class ImageUploadClient {
class func upload(image: UIImage, to request: URLRequest) {
let imgData = UIImageJPEGRepresentation(image, 0.5)!
let filename = "file.jpeg"
Alamofire.upload(multipartFormData: { (multiPartData) in
multiPartData.append(imgData, withName: filename, mimeType: "image/jpg")
}, usingThreshold: UInt64(1024),
with: request, encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let request, let streamingFromDisk, let fileURL):
debugPrint(streamingFromDisk) // Shows true
debugPrint(fileURL) // Returns file url
debugPrint(request)
// upload progress closure
request.uploadProgress(closure: { (progress) in
print("upload progress: \(progress.fractionCompleted)")
// here you can send out to a delegate or via notifications the upload progress to interested parties
})
// response handler
request.validate()
.responseJSON(completionHandler: { (response) in
switch response.result {
case .success(let value):
debugPrint(value)
case .failure(let err):
debugPrint(err)
}
})
// encodingResult failure
case .failure(let error):
debugPrint(error)
}
})
}
}
try by adding file name for your image
like this
and withName key will contain Key name to your image on server
let profileKey = "profileImage"
multiPartData.append(imgData, withName: profileKey, fileName: filename, mimeType: "image/jpg")

How to upload PDF file in swift using form-data or any other method

can we upload pdf/doc file using form-data in swift. Or any other way to upload. if possible please provide working example as i am new in this technology.
Its good to use excellent Alamofire library to upload any doc (such as pdf file).The below code will explain how to use alamofire to upload a file
Alamofire.upload(
multipartFormData: {
multipartFormData in
if let urlString = urlBase2 {
let pdfData = try! Data(contentsOf: urlString.asURL())
var data : Data = pdfData
multipartFormData.append(pdfData, withName: "pdfDocuments", fileName: namePDF, mimeType:"application/pdf")
for (key, value) in body {
multipartFormData.append(((value as? String)?.data(using: .utf8))!, withName: key)
}
print("Multi part Content -Type")
print(multipartFormData.contentType)
print("Multi part FIN ")
print("Multi part Content-Length")
print(multipartFormData.contentLength)
print("Multi part Content-Boundary")
print(multipartFormData.boundary)
}
},
to: url,
method: .post,
headers: header,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
print(" responses ")
print(response)
print("Responses ended")
onCompletion(true, "Something went wrong", 200)
}
case .failure(let encodingError):
print(encodingError)
onCompletion(false, "Something went wrong", 200)
}
})

Uploading image to third party service using alamofire and auth token

I have been trying to upload my image to a third party website using their api and alamofire to get some information back regarding the image that has been uploaded. I have to pass the auth token in with the request to get access to their resources, here is the instruction they have provided,
"Image should be specified by its url or uploaded with multipart form by parameter 'data'. You should use, respectively, GET and POST methods."
I have been trying to implement the upload method in 5 different ways but none of them seem to work I get a responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 \"Invalid value around character 2.\" UserInfo={NSDebugDescription=Invalid value around character 2.}))" error back here is one of the functions that takes care of the upload:
let headers: HTTPHeaders = [ "Authorization": "Bearer \(authToken)" ]
let URL = try! URLRequest(url: "url", method: .post, headers: headers)
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(imageData, withName: "signImg", fileName: "picture.png", mimeType: "image/png")
}, with: URL, encodingCompletion: {
encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint("SUCCESS RESPONSE: \(response)")
}
case .failure(let encodingError):
// hide progressbas here
print("ERROR RESPONSE: \(encodingError)")
}
})
I also don't understand the meaning of the parameters used to append the multipartformdata such as withName, filetype, mimeType.
I would really appreciate if you could attach an explanation with the solution.

Type of expression is ambiguous without more context in Alamofire.upload swift 3

Updated Alamofire 4.0.0 does not mention how to put Httpmethod & Httpheaders in upload with multipartFormData. That's why I google and found solution in that stackoverflow question. But the problem is I did same as that answer then got following error message and building is failed. Please help me how to solve it.
Type of expression is ambiguous without more context
Here is my coding:
let URL = try! URLRequest(url: Config.imageUploadURL, method: .post, headers: headers)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(self.imageData, withName: "image", fileName: "file.png", mimeType: "image/png")
},
to: URL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if((response.result.value) != nil) {
} else {
}
}
case .failure( _):
}
}
)
Alamofire.upload(multipartFormData:to:encodingCompletion:) takes a URLConvertible for the to: argument. Instead, you should use Alamofire.upload(multipartFormData:with:encodingCompletion:) which takes a URLRequestConvertible for its with: argument.
I think your argument name of URL that is the same as the type URL() helps in creating strange compiler errors.
The following compiles for me:
let url = try! URLRequest(url: URL(string:"www.google.com")!, method: .post, headers: nil)
Alamofire.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(Data(), withName: "image", fileName: "file.png", mimeType: "image/png")
},
with: url,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
if((response.result.value) != nil) {
} else {
}
}
case .failure( _):
break
}
}
)
For me the build error was caused by a multipartFormData.appendBodyData(). After replacing it with multipartFormData.append() the problem was solved.
I got the same error, after spending lots of time, i found that issue was:
I was passing MutableURLRequest instead of passing URLRequest object. Thats why i were getting this error. After type casting it to URLRequest, it start working.