how to use form data in Alamofire
If you're trying to pass parameters as multipart/form-data
you have to use the upload method in Alamofire
:
func fetchRegisterData() {
let parameters = ["mobile": mobilenumberTextfield.text!]
AF.upload(multipartFormData: { (multiFormData) in
for (key, value) in parameters {
multiFormData.append(Data(value.utf8), withName: key)
}
}, to: registerApi).responseJSON { response in
switch response.result {
case .success(let JSON):
print("response is :\(response)")
case .failure(_):
print("fail")
}
}
}
Using FORM DATA with Alamofire
So my solution is.... you have to specify the Parameter Encoding in Alamofire. So the code will look like this.
Swift 2.0
func registerNewUserFormData(completionHandler: (Bool, String?) -> ()){
// build parameters
let parameters = ["email": "test@test.cz", "password": "123456"]
// build request
Alamofire.request(.POST, urlDomain + "register", parameters: parameters, encoding: .URL).responseJSON { response in
switch response.result {
case .Success:
print("Validation Successful")
if let JSON = response.result.value {
print(JSON)
}
case .Failure(let error):
print(error)
}
}
}
How to make alamofire post request with form-data parameters
Try changing
multipartFormData.append(UIImagePNGRepresentation(imgToSend)!, withName: "image")
to
multipartFormData.append(UIImagePNGRepresentation(imgToSend)!, withName: "image", fileName: "sample.png", mimeType: "image/png")
If you're getting warning like:
line 2878 [boringssl_session_write] SSL_ERROR_SYSCALL(5): operation
failed externally to the library
You can simply ignore it. This simply means that an operation on the TLS connection failed because the TLS was closed via the close_notify
alert. This sort of thing is not a problem in and of itself.
You can disable OS logging in Xcode to make them go away. With your project window open, go to Project -> Scheme -> Edit Scheme... and add "OS_ACTIVITY_MODE" to the Environment Variables section and set its value to "disable". When you rerun the app those warnings should now not appear.
How do I make POST form-data request with Alamofire? - Swift
I found the solution! for anyone else who might be facing this problem, heres why:
You just have to specify the filename too.
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(Data("one".utf8), withName: "file", fileName: "landing.jpeg")
Related Topics
Optional Protocol Requirements, I Can't Get It to Work
How to Use Nscalendar Range Function Within Calendar
Using Enumerated with Foreach in Swiftui
How to Change an Inout Parameter from Within a Escaping Closure
Running Xcode Docc Documentation via Apache Locally on Macos
Sdwebimage, Swift: Sdwebimagerefreshcached Unresolved Identifier
Swiftui Custom View Repeat Forever Animation Show as Unexpected
How to Exit a Function Scope from Inner Function Using Swift
Problems with Unified Logging, Staticstring, Customstringconvertible and Description
How to Dynamically Hide Navigation Back Button in Swiftui
Create Record Only If Parent Exists in Vapor Using Fluent
Swiftui Published Updates Not Refreshing
Idiomatic Way to Test Swift Optionals
Parse Weird Bug in Swift That Causes Acl Write Permissions to Change to an Objectid