How do I get Alamofire to Encode this Url - it keeps dropping parameter

混江龙づ霸主 提交于 2019-12-12 03:09:29

问题


I am trying to get Alamofire to encode this url as below using URLEncoded

https://domain.com/rest/api/2/search?query=assignment=user123()%20order%20by%20lastUp%20desc

I have used the following code:

let endpoint = "https://domain.com/rest/api/2/search/"
 let params:[String:AnyObject] = ["query" : "","assignment" : "user123() order by lastUpdated desc"] 

but when Alamofire encodes the url it drops the "query" parameter altogether and gives me this:

https://domain.com/rest/api/2/search?assignment=user123()%20order%20by%20lastUp%20desc // query parameter missing

  1. I have tried changing the parameters to this:

    let endpoint = "https://domain.com/rest/api/2/search/" let params:[String:AnyObject] = ["query" : "assignment=user123() order by lastUp desc"]

however it encodes the "=" sign as %20%3D%20

Does anyone have a suggestion, how I can get this to work?


回答1:


I just rank a quick test with your URL, and here's my output URL:

https://domain.com/rest/api/2/search/?assignment=user123%28%29%20order%20by%20lastUpdated%20desc&query=

So as you can see, the query param has not been skipped, it's just placed at the end. The Alamofire class ParameterEncoding.swift sorts the keys alphabetically while constructing the URL.

Here's my code for reference:

    let endpoint = "https://domain.com/rest/api/2/search/"
    let params:[String:AnyObject] = ["query" : "","assignment" : "user123() order by lastUpdated desc"]

    Alamofire.request(.GET, endpoint, parameters: params)
        .responseData { response in


            if let str = response.request?.URLString {

                print("~~~URL~~~\n", str)

            } else {

                print("oops")
            }
    }

However, the main point here is that if your intention is to pass one key (query) and one value (assignment=user123...), then the = is right to be encoded to %20%3D%20.

Your server should decode this back to a = and use it as required.




回答2:


Looks like the only way to get it to work was just to not use parameters at all and put the full endpoint as is.

let endpointFull = https://domain.com/rest/api/2/search?query=assignment=user123()%20order%20by%20lastUp%20desc  

 Alamofire.request(.GET, endpointFull)

Would have been much nicer if we could specify the order of the parameters.




回答3:


Are you expecting any URL encoder to not encode the = to %3D in a query string?

Try this: http://meyerweb.com/eric/tools/dencoder/

When I enter this in the Encoder/Decoder above:

assignment=user123() order by lastUp desc

This is the output I get:

assignment%3Duser123()%20order%20by%20lastUp%20desc

Simply put, "=" is not URL safe and it will be encoded if provided in a parameter value in the query string



来源:https://stackoverflow.com/questions/38732455/how-do-i-get-alamofire-to-encode-this-url-it-keeps-dropping-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!