2

I need to upload an image to the server endpoint where the structure has to be as following:

    {
        "name": "",
        "description": "",
        "photo": imageFile
    }

How can I send such a request using Alamofire?

I tried this but the result is error with error message Invalid type in JSON write (NSConcreteData) This is my code :

    let imageData = UIImagePNGRepresentation(image)
    let base64String = imageData!.base64EncodedDataWithOptions([])
    
    let parameters = [
        "name": "name",
        "description": "desc",
        "photo": base64String
    ]
    
    let credentialData = "\(id):\(secret)".dataUsingEncoding(NSUTF8StringEncoding)!
    let base64Credentials = credentialData.base64EncodedStringWithOptions([])
    let headers = ["Authorization": "Basic \(base64Credentials)"]

    Alamofire.request(.POST, "", parameters: parameters, encoding: .JSON, headers: headers).responseJSON { (response) -> Void in
        print(response)
    }

And I followed this code

If there is another way to do please advice, thanks.

Community
  • 1
  • 1

1 Answers1

1

This method I'm using you might find it useful

let image : UIImage = image.image!
let imageData = UIImagePNGRepresentation(image)
let base64String = imageData!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
O-mkar
  • 5,430
  • 8
  • 37
  • 61