Alamofire POST request with progress

I am using Alamofire to execute a POST request. Since this POST request may take some time, and I want to track the progress and display it as a ProgressView.

Alamofire.request(.POST, ApiLink.create_post, parameters: parameters, encoding: .JSON) .progress { (bytesRead, totalBytesRead, totalBytesExpectedToRead) -> Void in println("ENTER .PROGRESSS") println("\(totalBytesRead) of \(totalBytesExpectedToRead)") self.progressView.setProgress(Float(totalBytesRead) / Float(totalBytesExpectedToRead), animated: true) } .responseJSON { (_, _, mydata, _) in println(mydata) } 

However, I noticed that the .progress block is called only after the mail request has ended, and not called several times to actually track the progress. println ("ENTER.PROGRESSS") is called only once (at the end)

How can I make .progress work with Alamofire.request POST?

Also: My options include a base64 encoded image string. I use back-end Ruby on Rails for image processing. This is a process that takes a lot of time.

+5
source share
3 answers

Instead, you can use the ParameterEncoding enumeration to generate HTTPBody data. Then you can pull this data and pass it to the Alamofire upload method. Here the version of your function is changed, which is compiled on the playground and uses the upload function instead.

 struct ApiLink { static let create_post = "/my/path/for/create/post" } let parameters: [String: AnyObject] = ["key": "value"] // Make sure this has your image as well let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: ApiLink.create_post)!) mutableURLRequest.HTTPMethod = Method.POST.rawValue let encodedURLRequest = ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0 let data = encodedURLRequest.HTTPBody! let progressView = UIProgressView() Alamofire.upload(mutableURLRequest, data) .progress { _, totalBytesRead, totalBytesExpectedToRead in println("ENTER .PROGRESSS") println("\(totalBytesRead) of \(totalBytesExpectedToRead)") progressView.setProgress(Float(totalBytesRead) / Float(totalBytesExpectedToRead), animated: true) } .responseJSON { _, _, mydata, _ in println(mydata) } 

This will certainly be updated as @mattt, originally mentioned in his comment above.

+6
source

Like @cnoon , you can use the download method to track progress with some changes. Here's exactly what worked with me:

 let jsonData = NSJSONSerialization.dataWithJSONObject(jsonObject, options: .PrettyPrinted) Alamofire.upload(.POST, "API URL String", headers: ["Content-Type": "application/json"], data: jsonData) .validate() .responseJSON(completionHandler: { (response: Response<AnyObject, NSError>) in //Completion handler code }) .progress({ (bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) in //Progress handler code }) 

Note that you set the "Content-Type" value of the " application / json " HTTP header if the data is formatted as json, which must be correctly decoded to the backend.

+1
source

Alamofire has a separate download method. Please check their documentation here .

They have subsections.

  • Download continues.
  • Download MultipartFormData li>

What the download describes.

0
source

Source: https://habr.com/ru/post/1212396/


All Articles