1

It's there any way to post JSON with progress in alamofire? what I need to do I have JSON which has base64 image string and some other parameters while requesting JSON I need to show Progress to end user, so anyone has an idea how to do it in Alamofire?

I have followed the below link, but it gives me a syntax error in Alamofire?

Alamofire POST request with progress

    let parameters: [String: AnyObject] = ["key": "value" as AnyObject]
    let mutableURLRequest = NSMutableURLRequest(url: URL(string: "url goes here")!)
    mutableURLRequest.httpMethod = "POST"

    let encodedURLRequest = try! Alamofire.URLEncoding.default.encode(mutableURLRequest as! URLRequestConvertible, with: parameters)
    let data = encodedURLRequest.httpBody!

    Alamofire.upload(mutableURLRequest, data)
        .progress { _, totalBytesRead, totalBytesExpectedToRead in
            print("ENTER .PROGRESSS")
            print("\(totalBytesRead) of \(totalBytesExpectedToRead)")
        }
        .responseJSON { _, _, mydata, _ in
            print(mydata)
    }

It gives me below error

Cannot invoke 'upload' with an argument list of type '(NSMutableURLRequet, Data)

Nics
  • 21
  • 4

2 Answers2

0

As you have pointed out that you can use the Alamofire to upload the file or image without converting it Base64 , using MultipartFormData and track the progress. Incase any syntax error you can tell us more about it or return back to the documentation.

  • Thanks @mohammad but I am not uploading a file, I am uploading base64 of image so I have to go with that, API only accept base64 string not Image – Nics Oct 09 '18 at 08:09
0

You can use below code if you want to show progress hud:

 let binaryImageData = base64EncodeImage(image)
 SVProgressHUD.show() //use any progress hud
  let parameters : [String:Any] = [
            "Name" : "UserProfilePhoto",
            "Body": binaryImageData,
            "parentId": userId

   ]
   let header = ["content-type" : "application/json", "Authorization": "your token here"]
   var imageURL: URL {
            return URL(string: "Your UrlString here")
        }
   Alamofire.request(imageURL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: header).responseJSON {
            response in

            if (response.result.isSuccess){
                SVProgressHUD.dismiss()
                let result = response.result.value as? NSDictionary
            }
            else{
                SVProgressHUD.dismiss()
            }
        }
    }
kdhingra7
  • 43
  • 6