Skip to content

Instantly share code, notes, and snippets.

@vzsg
Created July 26, 2017 09:28
Show Gist options
  • Save vzsg/b1a7405c987bdf18cc9035591ea2f58c to your computer and use it in GitHub Desktop.
Save vzsg/b1a7405c987bdf18cc9035591ea2f58c to your computer and use it in GitHub Desktop.
Alamofire+RX multipart encoding
import Foundation
import Alamofire
import RxSwift
extension Reactive where Base: SessionManager {
func encodeMultipartUpload(to url: URLConvertible, method: HTTPMethod = .post, headers: HTTPHeaders = [:], data: @escaping (MultipartFormData) -> Void) -> Observable<UploadRequest> {
return Observable.create { observer in
self.base.upload(multipartFormData: data,
to: url,
method: method,
headers: headers,
encodingCompletion: { (result: SessionManager.MultipartFormDataEncodingResult) in
switch result {
case .failure(let error):
observer.onError(error)
case .success(let request, _, _):
observer.onNext(request)
observer.onCompleted()
}
})
return Disposables.create()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment