1

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

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
  • You want that `/` be percent encoded in the `path`? I think, that even if you don't use "double encoding", but only one, Alamofire won't encode them. So it might not be related to double encoding, but just one. – Larme Nov 25 '20 at 13:21
  • 1
    You should escape them with either `.urlHostAllowed`, `.urlPasswordAllowed` or `.urlUserAllowed` which should percent encode it (see https://stackoverflow.com/questions/29806098/nscharacterset-urlhostallowedcharacterset-doesnt-replace-sign for the values). Alamofire doesn't seems to do so. – Larme Nov 25 '20 at 13:28
  • For some reason res["path"]?.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed leaves "v1/user_app/user/balance" as is: urlQueryAllowed does not encode slashes – Anton Tropashko Nov 25 '20 at 14:12
  • ugh https://tools.ietf.org/html/rfc3986 says slashes are perfectly valid in parameters and thusly I was trying to solve a problem I don't relly have :-[ – Anton Tropashko Nov 25 '20 at 14:19

0 Answers0