title: How can I pass body parameters along with file in multipart file upload using Alamofire in Swift
body: <p>I am working on multi part file upload with Alamofire Library in Swift. I need to pass some parameters along with the file. The following is the body I need to pass to the server: </p>
  
  <pre><code>{
    "eventId":7,
    "eventImageList":[
      { "contentType":"png", "imageName":"test", "imagePath":"" }
      ,
      { "contentType":"png", "imageName":"test2", "imagePath":"" }
    ]
  }
  </code></pre>
  
  <p>I have tried similar to this:</p>
  
  <pre><code>let image = UIImage(named: "sampleImage")
  
  Alamofire.upload(.POST, URL, multipartFormData: {
      multipartFormData in
          if let _image = image {
              if let imageData = UIImageJPEGRepresentation(_image, 0.5) {
                  multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.png", mimeType: "image/png")
              }
          }
          for (key, value) in dictionary {
              multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key as! String)
          }
  
      }, encodingCompletion: {
          encodingResult in
  
          switch encodingResult {
          case .Success(let upload, _, _):
              upload.responseJSON { response in
  
                  switch response.result {
                  case .Success:
                       print("jsonResponse ==== ", response)
                  case .Failure(let error):
                       print("error ==== ", error)
                  }
              }
          case .Failure(let encodingError):
              print(encodingError)
          }
  })
  </code></pre>
  
  <p>I need to pass this array:</p>
  
  <pre><code>[
    { "contentType":"png", "imageName":"test", "imagePath":"" }
    ,
    { "contentType":"png", "imageName":"test2", "imagePath":"" }
  ]  
  </code></pre>
  
  <p>but when I decode it like below, the app crashes.</p>
  
  <pre><code>multipartFormData.appendBodyPart(data:value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
  </code></pre>