title: How to upload an image using alamofire multipart form data with basic authentication?
body: <p>I am new in programming and in iOS development. I need to upload an image using Alamofire multipart form data, but I also need to input basic authentication header.</p>
  
  <p>in this thread <a href="https://stackoverflow.com/questions/39630997/alamofire-4-0-upload-multipartformdata-header">Alamofire 4.0 Upload MultipartFormData Header</a>, it seems similar to my problem, the code is like this to upload</p>
  
  <pre><code>Alamofire.upload(multipartFormData:{ multipartFormData in
           multipartFormData.append(unicornImageURL, withName: "unicorn")
           multipartFormData.append(rainbowImageURL, withName: "rainbow")},
         usingThreshold:UInt64.init(),
         to:"https://httpbin.org/post",
         method:.post, 
         headers:["Authorization": "auth_token"], 
         encodingCompletion: { encodingResult in
          switch encodingResult {
          case .success(let upload, _, _):
              upload.responseJSON { response in
                  debugPrint(response)
              }
          case .failure(let encodingError):
              print(encodingError)
          }
      })
  </code></pre>
  
  <p>but I am confused how to put the basic authentication (i.e username &amp; password) header. and I also confused where I should place my image data.</p>
  
  <p>I find another thread that seems similar to my problem. here it is... <a href="https://stackoverflow.com/questions/34375376/how-to-upload-multipartformdata-with-authentication-using-alamofire/34378366">How to upload MultipartFormData with authentication using Alamofire</a>, the proposed solution is this code :</p>
  
  <pre><code>let username = "username"
  let password = "password"
  
  let credentialData = "\(username):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!                  
  let base64Credentials = credentialData.base64EncodedStringWithOptions([])
  let headers = ["Authorization": base64Credentials]
  
  Alamofire.upload(
      .POST,
      "https://rilbits.com/supersafe/photo/upload",
      headers: headers,
      multipartFormData: { multipartFormData in
          let data = "default".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
          multipartFormData.appendBodyPart(data: data, name: "_formname")
          multipartFormData.appendBodyPart(fileURL: fileURL, name: "photo")
      },
      encodingCompletion: { encodingResult in
          switch encodingResult {
  
          case .Success(let upload, _, _):
              upload.responseString { response in
                  debugPrint(response)
              }
  
          case .Failure(let encodingError):
              print(encodingError)
          }
      }
  )
  </code></pre>
  
  <p>but after fixing to the swift 4.1 it gives an error :</p>
  
  <p><a href="https://i.stack.imgur.com/Z9dqf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z9dqf.png" alt="enter image description here"></a></p>
  
  <p>could you please help my problem? Thanks in advance :)</p>