How to pass nil value for one of the parameters in alamofire Post request

I would like to pass the value nil, that is, optionally for one of the parameter values. And it should start with nil in the Alamofire Post request. It would be helpful if you told me how to proceed next?

let image: UIImage = UIImage() let imageData = UIImagePNGRepresentation(image) let base64String = imageData?.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) let parameters = [ "first_name": "XXXXX", "email" : " 1234@gmail.com ", "password" : "password", "profile_picture" : base64String] Alamofire.request(.POST, "http://abc/public/user/register", parameters: parameters, encoding: .JSON, headers: nil) .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in print(totalBytesWritten) // This closure is NOT called on the main queue for performance // reasons. To update your ui, dispatch to the main queue. dispatch_async(dispatch_get_main_queue()) { print("Total bytes written on main queue: \(totalBytesWritten)") } } .responseJSON { response in debugPrint(response) } 

The answer should succeed even if profile_pictures is empty. I know that this can be done with an extra chain, but I don’t know how to proceed!

+5
source share
4 answers

By passing a null or uninitialized optional parameter, the server will receive an optional

You can pass NSNull () to a dictionary

try this for example

 var params = ["paramA","valueA"] if imageBase64 == nil { parms["image"] = NSNull()} else { params["image"] = imageBase64 } 

swiftyjson also treats null as NSNull

also have a nice link here null / nil in fast language

+5
source

I think your easiest answer is to add "profile_picture" as a second step.

 var parameters = [ "first_name": "XXXXX", "email" : " 1234@gmail.com ", "password" : "password"] if let base64String = base64String where !base64String.isEmpty { parameters["profile_picture"] = base64String } 
+1
source

hope this helps you :)

 var parameters :[String : AnyObject?] = [ "first_name": "XXXXX", "email" : " 1234@gmail.com ", "password" : "password"] if let string = base64String where base64String.isEmpty != false { parameters["profile_picture"] = string } 
-1
source

After a very thorough study, I found out that this can be done easily with the help of an additional chain and type casting.

The first step: to separate the parameters by type, pointing it at the string and Image , and check for the String value and the Image value.

 let parameters: [String : AnyObject? ] = [ "first_name": "XXXXX", "email" : " 1234@gmail.com ", "password" : "password", "profile_pic" : UIImage(named: "logo")] 

Do it in the request method

 for (key, value) in parameters { if let value1 = value as? String { multipartFormData.appendBodyPart(data: value1.dataUsingEncoding(NSUTF8StringEncoding)!, name: key) } if let value1 = value as? UIImage { let imageData = UIImageJPEGRepresentation(value1, 1) multipartFormData.appendBodyPart(data: imageData!, name: key, fileName: "logo.png" , mimeType: "image/png") } 

You do not need to divide the parameters into two, one for string values ​​and the other for the image, and you do not need to convert the image to a string. Hope this solution helps!

-1
source

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


All Articles