Welcome King III Welcome King III - 1 month ago 20
iOS Question

Alamofire 4 swift 3 Uploading MultipartFormData with header not posting

I'm having a problem with my code and the new Alamofire 4 have searched the web for examples - no luck. My code skips the post method it's not calling to the URL:

Alamofire.upload(multipartFormData:{
multipartFormData in
multipartFormData.append(tempUrl, withName: self.path)},
usingThreshold:UInt64.init(),
to:posturl!,
method:.post,
headers: Oauthdata.uplodHeader ,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})} catch {
print("Somethings not right")
}

Answer

Try the approach using RequestAdapter protocol, mentioned in the docs:

class AccessTokenAdapter: RequestAdapter {
    private let accessToken: String

    init(accessToken: String) {
        self.accessToken = accessToken
    }

    func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
        var urlRequest = urlRequest
        urlRequest.setValue("Bearer " + accessToken, forHTTPHeaderField: "Authorization")
        return urlRequest
    }
}

class HTTPClient {

    func setupHeader(with authToken: String) {
        let sessionManager = SessionManager()
        sessionManager.adapter = AccessTokenAdapter(accessToken: authToken)
    }
}

Where HTTPClient is the class where you are performing this setup...