I have some problem when try to call a url string request with some special character inside. For example with CharacterSet, when in URL have the %20
that mean spacebar and I use the addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
function it's replaced to %2520
. Can you guys try this code:
let url = "http://analytics.abc.io/acct?start_date=2017-11-15&aff=ABC%20Telecom"
url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print(URL(string: url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)!)
Actually it's request sucessfully but the response data is wrong. Any guys can give me some ideas and how to solve that clearly.
Beside that please give me some comment about the URLQueryItem I not use that, so I need your help. Thank and cheer!
Honestly, is it 100% much better if you decompose using URLComponents. It will facilitate the work for you and the next dev to read the code.
let host = "analytics.abc.io"
let scheme = "http"
let path = "/acct"
var urlQueryItems : [URLQueryItem] = []
for (key, value) in [("start_date", "2017-11-15"), ("aff", "ABC Telecom")] {
urlQueryItems.append(URLQueryItem(name: key, value: value))
}
var result = URLComponents()
result.host = host
result.scheme = scheme
result.path = path
result.queryItems = urlQueryItems
print(result.url)
//prints : Optional(http://analytics.abc.io/acct?start_date=2017-11-15&aff=ABC%20Telecom)