Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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~~~
", 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...