Nick Barr
Nick Barr

Reputation: 574

Can't upload an image as an attachment from iOS via Trello API

I'm trying to upload an image from my iOS app to Trello using the API:

      let imageDataToSend = UIImageJPEGRepresentation(imageToSend, 0.9)

      let parameters: [String: AnyObject] = [
        "file": imageData,
        "mimeType" : "image/jpeg",
        "token" : "mytoken",
        "key" : "mykey",
        "name" : "attachment"

    ]

    oauthswift.client.post("https://trello.com/1/cards/\(card)/attachments", parameters: parameters,
                           success: {
                            data, response in
                            print("attach worked \(self.nsdataToJSON(data))")


        }, failure: { (error) in
            print("post failed\(error)")
    })

The attachment always comes through as a text file. I've tried 64bitEncoding, UIImagePNGRepresentation. I've tried setting different mimeTypes.

I don't understand why the mimeType always comes back null. Here's the data that I get back:

Optional({
bytes = 20681;
date = "2016-05-09T03:33:44.151Z";
edgeColor = "<null>";
id = 57300518f5160afdb6ae5670;
idMember = 54ef9a89772213529008b0a9;
isUpload = 1;
mimeType = "<null>";
name = attachment;
previews =     (
);
url = "https://trello-attachments.s3.amazonaws.com/56b8cf1bca0ff64f7f1ba86a/57300517b871603e15aeb9b5/b8b906a4c952ec3b8d889fcfa37dddfb/Upload";
})

Here is the relevant API: https://developers.trello.com/advanced-reference/card#post-1-cards-card-id-or-shortlink-attachments

Upvotes: 1

Views: 720

Answers (2)

Nick Barr
Nick Barr

Reputation: 574

I was able to resolve this using a different library — Alamofire and Trello seem to play nice together. Here's the method, if anyone runs into similar issues:

    Alamofire.upload(.POST, URL, multipartFormData: {
        multipartFormData in

            if let imageData = UIImageJPEGRepresentation(image, 0.5) {
                multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.png", mimeType: "image/png")
            }

        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }

        }, encodingCompletion: {
            encodingResult in

            switch encodingResult {
            case .Success(let upload, _, _):
                print("it worked")
            case .Failure(let encodingError):
                print(encodingError)
            }
    })

Upvotes: 0

sohan vanani
sohan vanani

Reputation: 1574

When submitting parameters like this, you should percent escape the values. For example, you might define a character set to be only the "unreserved" characters (as defined by RFC 3986), namely the alphanumeric characters plus -, ., _, and ~: Add extension to your swift file like this:

extension NSCharacterSet {
    class func URLParameterValueCharacterSet() -> NSCharacterSet {
        let characterSet = NSMutableCharacterSet.alphanumericCharacterSet()
        characterSet.addCharactersInString("-._~")

        return characterSet
    }
}

Then you can do something like:

func percentEscapeString(string: String) -> String {
        return string.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLParameterValueCharacterSet())!
    }

And now post your image like this:

  var imageData = UIImageJPEGRepresentation(selectedImg, 0.2)
  let base64String = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
  let imgStringToPost = percentEscapeString(base64String)
  let parameters: [String: AnyObject] = [
        "file": imgStringToPost,
        "mimeType" : "image/jpeg",
        "token" : "mytoken",
        "key" : "mykey",
        "name" : "attachment"

    ]

Upvotes: 1

Related Questions