title: Swift 4 - Alamofire 4 Upload Image with Parameter
body: <p>I know there's so many tutorials or questions about this. I've tried that all but I have no luck for this.</p>
  
  <p>I need to get upload progress from <code>Alamofire</code>. Here's my code:</p>
  
  <pre><code>func uploadpp(data1: Array&lt;Any&gt;, data2: Data?){
      guard let url = URL(string: "\(API.storage)/upload/profile/picture?format=json") else {
          return
      }
  
      let parameters: Parameters = [
          "crop": [],
          "folder_type": "1",
          "folderid": "0",
          "image_byte": data1,
          "image_url": "",
          "partnerid": "\(memberid)",
          "partneruserid": "0",
          "publicip": "",
          "sourceid": "4"
      ]
  
      let headers: HTTPHeaders = [
          "Authorization": API.basicstring,
          "Content-type": "multipart/form-data"
      ]
  
      Alamofire.upload(multipartFormData: { (multipartFormData) in
          if let data = data2 {
              multipartFormData.append(data, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
          }
  
          for (key, value) in parameters {
              multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
          }
      }, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
          switch result {
          case .success(let upload, _, _):
              upload.uploadProgress(closure: { (progress) in
                  print("proses", progress.fractionCompleted)
              })
  
              upload.responseJSON { response in
                  print("Succesfully uploaded")
                  if let err = response.error {
                      print("upload:", err)
                      return
                  }
  
                  if let s = response.result.value {
                      print("result:", s)
                      return
                  }
              }
          case .failure(let error):
              print("Error in upload: \(error.localizedDescription)")
          }
      }
  }
  </code></pre>
  
  <p><code>data1</code> is a byte array of image. I need this because my API have to read this type. And <code>data2</code> is result of <code>UIImageJPEGRepresentation(img, 0.5)</code>. </p>
  
  <p>Then when I tried this, it printed <code>Successfully uploaded</code> but then gave me error: </p>
  
  <blockquote>
    <p>responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 3." UserInfo={NSDebugDescription=Invalid value around character 3.}))</p>
  </blockquote>
  
  <p>As I know, this error appears when you make mistake on your url, parameters, or headers (CMIIW please).</p>
  
  <p>But I've tried to put this to <code>Alamofire.request</code> so here's my another code:</p>
  
  <pre><code>func uploadpp(data: Array&lt;Any&gt;){
      guard let url = URL(string: "\(API.storage)/upload/profile/picture?format=json") else {
          return
      }
  
      let parameters: Parameters = [
          "crop": [],
          "folder_type": "1",
          "folderid": "0",
          "image_byte": data,
          "image_url": "",
          "partnerid": "\(memberid)",
          "partneruserid": "0",
          "publicip": "",
          "sourceid": "4"
      ]
  
      let headers: HTTPHeaders = [
          "Authorization": API.basicstring,
          "Accept": "application/json"
      ]
  
      let req = Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
      req.downloadProgress { progress in
          print("lalala", Float(req.progress.fractionCompleted), progress.completedUnitCount)
      }
  
      req.responseJSON { response in
          if response.result.isSuccess {
              _ = self.getdata(json: JSON(response.result.value!))
              print(response.result.value!)
          }
          else {
              print("upload:", response.result.error!)
          }
      }
  }
  </code></pre>
  
  <p>It was succesful but didn't give me the progress. It just gave "1.0".</p>
  
  <p>Maybe I confused about usage of <code>Alamofire.request</code> and <code>Alamofire.upload</code>, or somewhere from this all. So please somebody help me with this.</p>
  
  <p>Thanks in advance.</p>