Alamofire 3 Custom Encoding To Alamofire 4 Custom Encoding
In Alamofire 4.0 you should use ParameterEncoding
.
struct CustomEncoding: ParameterEncoding {
func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try! URLEncoding().encode(urlRequest, with: parameters)
let urlString = request.url?.absoluteString.replacingOccurrences(of: "%5B%5D=", with: "=")
request.url = URL(string: urlString!)
return request
}
}
Alamofire 4 Swift 3 ParameterEncoding Custom
In Alamofire 4.0 you should use ParameterEncoding
protocol. Here is an example, which makes any String
UTF8
encodable.
extension String: ParameterEncoding {
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try urlRequest.asURLRequest()
request.httpBody = data(using: .utf8, allowLossyConversion: false)
return request
}
}
Alamofire.request("http://mywebsite.com/post-request", method: .post, parameters: [:], encoding: "myBody", headers: [:])
Alamofire Custom Parameter Encoding
You have a couple questions in here. Let's break them down 1x1.
Compiler Issue
Your compiler issue is due to the fact that your return tuple is the wrong type. In Alamofire 1.3.0, we changed the return type to be an NSMutableURLRequest
which ends up making things much easier overall. That should fix your compiler issue.
Setting the HTTPBody
Now you have a couple options here.
Option 1 - Encode Data as JSON
let options = NSJSONWritingOptions()
let data = try NSJSONSerialization.dataWithJSONObject(parameters!, options: options)
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
mutableURLRequest.HTTPBody = data
From what you posted I'm assuming you actually want .URL encoding for the parameters.
Option 2 - Use the .URL Case
let parameters: [String: AnyObject] = [:] // fill in
let encodableURLRequest = NSURLRequest(URL: URL)
let encodedURLRequest = ParameterEncoding.URL.encode(encodableURLRequest, parameters).0
let mutableURLRequest = NSMutableURLRequest(URL: encodedURLRequest.URLString)
mutableURLRequest.HTTPMethod = "POST"
mutableURLRequest.setValue("text/html; charset=utf-8", forHTTPHeaderField: "Content-Type")
Alamofire.request(mutableURLRequest)
.response { request, response, data, error in
print(request)
print(response)
print(error)
}
Hopefully that helps get you going. Best of luck!
Alamofire with custom parameter encoding for swift application
The custom
closure is not executed by design, because there are no parameters to encode. He're a code excerpt taken from Alamofire.swift source file:
if parameters == nil {
return (URLRequest.URLRequest, nil)
}
As you can see, you can bybass this condition by passing an empty dictionary:
Alamofire.request(.POST, WebServiceURLString, parameters: Dictionary(), encoding: .Custom(custom))
The custom
closure will now be executed.
Related Topics
How Does Let X Where X.Hassuffix("Pepper") Work
Swiftui: Using View Modifiers Between Different iOS Versions Without #Available
Com.Apple.Itunes Aedeterminepermissiontoautomatetarget Is Always Return -600
How to Use Key-Value Observing with Smart Keypaths in Swift 4
Xcode 8 Shell Script Invocation Error
Sending an Email from Your App with an Image Attached in Swift
Swift Safely Unwrapping Optinal Strings and Ints
How to Observe Array Property Changes in Rxswift
How to Check If Airpods Are Connected to Iphone
Swift Packages and Conflicting Dependencies
Programmatically Create an Nsviewcontroller Without an Xib in Swift 3
How Do We Implement Wait/Notify in Swift
Can Nscoding and Codable Co-Exist
How to Change Default Background Color of Callout Bubble with Detailcalloutaccessoryview