1

I'm trying to do a POST request to an endpoint with body parameters img (an image) and id (can be any number).

This works in Postman (the server never responds with fail/success though unless when the id is missing):

enter image description here

However, on the iOS app side:

class func postPreviewImage(operaID: Int, image: UIImage, completion: @escaping () -> Void) {
    let URL_CORDINATE = "http://artaugmentedreality.com/api/postpreview/"
    print("Trying to post a preview of opera with ID:", operaID)

    guard let imgData = image.jpegData(compressionQuality: 0.2) else {
        print("Error while retrieving image data")
        return
    }

    Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(imgData, withName: "img", fileName: "\(Date().timeIntervalSince1970).jpg", mimeType: "image/jpeg")
        multipartFormData.append(operaID.data, withName: "id")
    },
                     to: URL_CORDINATE,
                     method: .post)
    { (result) in
        print("Result of Alamofire call is", result)

        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                print("Upload Progress: \(progress.fractionCompleted)")
            })
        case .failure(let encodingError):
            print("Error while uploading image:", encodingError)
        }
    }
}

extension Int {
    var data: Data {
        var int = self
        return Data(bytes: &int, count: MemoryLayout<Int>.size)
    }
}

it is uploading but somehow the server is not receiving it. Could there be something wrong in my request code? I'm getting a:

Error copying matching creds.  Error=-25300, query={
atyp = http;
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = http;
"r_Attributes" = 1;
sdmn = "artaugmentedreality.com";
srvr = "artaugmentedreality.com";
sync = syna;
}

error code from Alamofire, perhaps because the domain isn't HTTP. However, I included the domain in the list of allowed domains in info.plist:

enter image description here

So I'm clueless about how to go from here.

Cesare
  • 9,139
  • 16
  • 78
  • 130
  • Hi @Cesare! I have already given the answer at https://stackoverflow.com/questions/50489453/swift-4-alamofire-multipart-upload-not-working/50502614#50502614 – Ahtazaz May 02 '19 at 07:34
  • Thanks! I see you're passing all parameters as [String: String], however, my id is an integer. Do you think it matters if I'm passing an integer data instead of string data in the body parameters? – Cesare May 02 '19 at 07:36
  • I exactly don't know, but I think it might cause the problem... – Ahtazaz May 02 '19 at 07:41
  • 1
    can't see this key "Allow Arbitrary Loads" in a plist file. – Muhammad Shauket May 02 '19 at 07:48
  • @ShauketSheikh added now but doesn't change things. – Cesare May 02 '19 at 07:55

1 Answers1

2

You are sending ID parameter along with the Image. So try to use utf8 encoding.

 multipartFormData.append(operaID.data(using: .utf8)!, withName: "id")
Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58