user3517546
user3517546

Reputation: 143

How to uploading image with Alamofire and referencing the file in params

I can send image to an API (for moderation) and link it to a 'media' param in postman like this:

enter image description here

Now I'm trying to do the same in swift. I followed the example here but got error: Media not specified. Heres my code: Is the appended image file not linked to the media param correctly?

    let image = self.descriptionImage.image!
    let parameters = [
        "api_user": "xxxxx",
        "api_secret": "xxxxx",
        "models": "nudity,wad",
        "media": "file.png"
    ]

    Alamofire.upload(multipartFormData: { multipartFormData in
        if let imageData = UIImageJPEGRepresentation(image, 1) {
            multipartFormData.append(imageData, withName: "file.png", fileName: "file.png", mimeType: "image/png")
        }

        for p in parameters {
            let value = p.value
            multipartFormData.append((value.data(using: .utf8))!, withName: p.key)
        }}, to: "https://api.sightengine.com/1.0/check.json", method: .post, headers: nil,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.response { [weak self] response in
                        guard let strongSelf = self else {
                            return
                        }
                        print(response.data)
                        print("strongSelf")

                        debugPrint(response)

                    }
                case .failure(let encodingError):
                    print("error:\(encodingError)")
                }
    })

Upvotes: 0

Views: 1245

Answers (1)

chengsam
chengsam

Reputation: 7405

The parameter for the image should be specified in the multipart form data, try change the code to below:

let image = self.descriptionImage.image!
let parameters = [
    "api_user": "xxxxx",
    "api_secret": "xxxxx",
    "models": "nudity,wad"
]

Alamofire.upload(multipartFormData: { multipartFormData in
    if let imageData = UIImagePNGRepresentation(image) {
        multipartFormData.append(imageData, withName: "media", fileName: "file.png", mimeType: "image/png")
    }

    for p in parameters {
        let value = p.value
        multipartFormData.append((value.data(using: .utf8))!, withName: p.key)
    }}, to: "https://api.sightengine.com/1.0/check.json", method: .post, headers: nil,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.response { [weak self] response in
                    guard let strongSelf = self else {
                        return
                    }
                    print(response.data)
                    print("strongSelf")

                    debugPrint(response)

                }
            case .failure(let encodingError):
                print("error:\(encodingError)")
            }
})

Upvotes: 2

Related Questions