Alamofire 5: Multiple encoding types for Request

565 Views Asked by At

similar to this Multiple encoding types for Alamofire Request

and this Multiple encoding types for request in Alamofire 4

I need to get some parameters into query string and some json into htpbody

so I have something like this

    public func asURLRequest() throws -> URLRequest
    {
        let combined = baseUrl.absoluteString + path
        guard let url_ = URL(string: combined) else {
            print ("invalid url \(combined)")
            throw AFError.parameterEncodingFailed(reason: .missingURL)
        }
        guard var components = URLComponents(url: url_, resolvingAgainstBaseURL: false) else {
            print ("invalid url \(url_)")
            throw AFError.parameterEncodingFailed(reason: .missingURL)
        }
//        var items: [URLQueryItem] = components.queryItems ?? []
//        for (key, value) in query_parameters ?? [:]{
//            guard let value = value as? String else {
//                assertionFailure()
//                continue
//            }
//            let item = URLQueryItem(name: key, value: value)
//            items.append(item)
//        }
//        components.queryItems = items
        guard let url = components.url else {
            print ("invalid compontns \(components)")
            throw AFError.parameterEncodingFailed(reason: .missingURL)
        }
        let originalRequest: URLRequest = try URLRequest(url: url, method: method, headers: headers)
        let params: Parameters = parameters
        let uRLRequest = try parameterEncoding.encode(originalRequest, with: params)
        if let query_parameters = query_parameters {
            let encodedURLRequest = try URLEncoding.default.encode(uRLRequest, with: query_parameters)
            return encodedURLRequest
        }
        return uRLRequest
    }

but the query parameters are not encoded:

https://somehostsomewhere/imobile/LoyaltyProxy?language=ru&path=v1/user_app/user/balance

path has slashes in it :-[

The discussion here https://github.com/Alamofire/Alamofire/issues/374 has died in 2017

i want to be mistaken but I suspect there's gonna be a thread for alamofire 6 and 7 and 8 in the future

0

There are 0 best solutions below