Alamofire 4.0 Upload MultipartFormData Header
Asked Answered
G

1

23

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?

Giraldo answered 22/9, 2016 at 5:34 Comment(0)
S
80

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.

Sympathize answered 22/9, 2016 at 6:51 Comment(10)
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.Sympathize
#39632323.Sympathize
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.MultipartEncodingFailureReason.bodyPartFileNotReachableWithError(file:///var/mobile/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?Hoffmann
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!!Seaborg
how to add parameters with upload,can you halp me.Transfuse
@Transfuse yes. What you want ?Sympathize
i want to pass - method: .post, parameters: parameters, headers: ["Authorization": "auth_token"], using Alamofire.upload()Transfuse
@Transfuse please check my answer here. #39810367Sympathize
hi @EktaPadaliya , could you please help my problem in uploading using alamofire ? tahnks in advance , maybe you have the answer. #50327562Heterochromosome
let url = try! URLRequest(url: URL(string: urlString)!, method: .post, headers: headers)Tabu

© 2022 - 2024 — McMap. All rights reserved.