Alamofire Type 'Parameterencoding' Has No Member 'Url' Swift 3

Alamofire Type 'ParameterEncoding' has no member 'URL' Swift 3

I'd change the name of this computed property to, say, request, to avoid clashing with the new type name, URLRequest. Coincidentally, this computed property should use a type of URLRequest:

public var request: URLRequest {
let url = URL(string: Router.baseURL)!
.appendingPathComponent(Router.basePath)
.appendingPathComponent(path)

var request = URLRequest(url: url)
request.httpMethod = method.rawValue

var parameters = [String: Any]()
parameters["key"] = Router.key
parameters["hl"] = "en"

switch self {
case .getMostPopularVideos(let pageToken):
parameters["part"] = "snippet,contentDetails,statistics"
parameters["chart"] = "mostPopular"
parameters["videoCategoryId"] = TubeTrends.Settings.topTrendsCat
if let pageToken = pageToken {
parameters["pageToken"] = pageToken
}
return try! Alamofire.URLEncoding.default.encode(request, with: parameters)
}
}

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: [:])

How to add Alamofire URL parameters

The problem is that you're using URLEncoding.default. Alamofire interprets URLEncoding.default differently depending on the HTTP method you're using.

For GET, HEAD, and DELETE requests, URLEncoding.default encodes the parameters as a query string and adds it to the URL, but for any other method (such as POST) the parameters get encoded as a query string and sent as the body of the HTTP request.

In order to use a query string in a POST request, you need to change your encoding argument to URLEncoding(destination: .queryString).

You can see more details about how Alamofire handles request parameters here.

Your code should look like:

   _url = "http://localhost:8080/"
let parameters: Parameters = [
"test": "123"
]

Alamofire.request(_url,
method: .post,
parameters: parameters,
encoding: URLEncoding(destination: .queryString),
headers: headers)

How to use `ParameterEncoding` together with `URLRequestConvertible`?

That's how Alamofire handles it internally :

open func request(
_ urlString: URLStringConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: [String: String]? = nil)
-> DataRequest
{
let urlRequest = URLRequest(urlString: urlString, method: method, headers: headers)

do {
let encodedURLRequest = try encoding.encode(urlRequest, with: parameters)
return request(resource: encodedURLRequest)
} catch {
let request = self.request(resource: urlRequest)
request.delegate.error = error
return request
}
}

Basically, it uses the request without the parameters. I don't know how far it can be used in your own implementation (or How to migrate Alamofire router class to Swift 3? 's OP).
I'd suggest filing an issue with Alamofire if this is not possible. The new ParameterEncoding is literally two days old (PR 1465) and still in the 4.0.0 beta cycle.
In any case, using URLEncoding.encode() seldom fails if you set your own URLRequest because the only error thrown is when there's no URL provided with the request.

EDIT: Here you go, 4.0.0 released, and the issue's been fixed! (PR 1505). There have been other changes to URLRequestConvertible but all's in the Migration Guide and README.

Alamofire error after Swift 3.0 migration: Cannot convert value of type '(URL, HTTPURLResponse)- (URL)' to expected argument type 'Parameters' ?


 1. Simple Typo

RequestManager.mediaDownloadAlamofireManager.download(mediaSourceURI,
method: .get, parameters: destination)

with

RequestManager.mediaDownloadAlamofireManager.download(mediaSourceURI,
method: .get, destination: destination)



2. Change in DownloadFileDestination-Type

Alamofire 4.0 changed the type of DownloadFileDestination – the parameter that you call destination. It changed from:

(URL, HTTPURLResponse) -> (URL)

to

(URL, HTTPURLResponse) -> (URL, DownloadRequest.DownloadOptions)

So you also need to change your method to something like this:

let destination: (URL, HTTPURLResponse) -> (URL, DownloadRequest.DownloadOptions) = {
(temporaryURL, response) in

if let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first, let suggestedFilename = response.suggestedFilename {
filePath = directoryURL.appendingPathComponent("\(suggestedFilename)")
return (filePath!, [.removePreviousFile, .createIntermediateDirectories])
}
return (temporaryURL, [.removePreviousFile, .createIntermediateDirectories])
}

Please note that you no longer just return the url, but also have more control over how Alamofire stores files on the file system. Just specify the options you want, such as removePreviousFile or/and createIntermediateDirectories. They are now returned as a Tuple.



Related Topics



Leave a reply



Submit