Alamofire 4.0 Upload MultipartFormData Header

23,194

I got the solution.

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)
        }
    })

Hope it will help you.

Share:
23,194

Related videos on Youtube

JayVDiyk
Author by

JayVDiyk

Updated on January 10, 2020

Comments

  • JayVDiyk
    JayVDiyk over 4 years

    How do we add an authentication header to the upload function of Alamofire 4.0?

    below is the sample code, however I see no way in adding a header to the function.

    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(unicornImageURL, withName: "unicorn")
            multipartFormData.append(rainbowImageURL, withName: "rainbow")
        },
        to: "https://httpbin.org/post",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )
    

    Previous version of alamofire supported adding header directly, but not the new one. Any ideas?

  • Ekta Padaliya
    Ekta Padaliya over 7 years
    Thanks for accepting. I have one question related image upload with its progress. If you know then can you please help me? I have added question on SO now.
  • Ekta Padaliya
    Ekta Padaliya over 7 years
  • Saeid
    Saeid over 7 years
    how about audio file in Alamofire 4? - i try to send a audio file in this form : multipartFormData.append(audioLocalPath, withName: "file", fileName: "file", mimeType: "application/octet-stream") but occur this error : multipartEncodingFailed(Alamofire.AFError.MultipartEncodingF‌​ailureReason.bodyPar‌​tFileNotReachableWit‌​hError(file:///var/m‌​obile/Containers/...‌​.... /Documents/item.mp3, NSUnderlyingError=0x16049100 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}})) - where is problem? -bad request or bad audio path?
  • user3069232
    user3069232 over 6 years
    Ekta, I know I shouldn't put comments like this, but I had too. This small piece of code was very useful! worked perfectly!! THANK YOU!!
  • Malleswari
    Malleswari about 6 years
    how to add parameters with upload,can you halp me.
  • Ekta Padaliya
    Ekta Padaliya about 6 years
    @Malleswari yes. What you want ?
  • Malleswari
    Malleswari about 6 years
    i want to pass - method: .post, parameters: parameters, headers: ["Authorization": "auth_token"], using Alamofire.upload()
  • Ekta Padaliya
    Ekta Padaliya about 6 years
    @Malleswari please check my answer here. stackoverflow.com/questions/39809867/…
  • sarah
    sarah almost 6 years
    hi @EktaPadaliya , could you please help my problem in uploading using alamofire ? tahnks in advance , maybe you have the answer. stackoverflow.com/questions/50327062/…
  • Puji Wahono
    Puji Wahono about 5 years
    let url = try! URLRequest(url: URL(string: urlString)!, method: .post, headers: headers)