Swift GET request with parameters
When building a GET
request, there is no body to the request, but rather everything goes on the URL. To build a URL (and properly percent escaping it), you can also use URLComponents
.
var url = URLComponents(string: "https://www.google.com/search/")!url.queryItems = [ URLQueryItem(name: "q", value: "War & Peace")]
The only trick is that most web services need +
character percent escaped (because they'll interpret that as a space character as dictated by the application/x-www-form-urlencoded
specification). But URLComponents
will not percent escape it. Apple contends that +
is a valid character in a query and therefore shouldn't be escaped. Technically, they are correct, that it is allowed in a query of a URI, but it has a special meaning in application/x-www-form-urlencoded
requests and really should not be passed unescaped.
Apple acknowledges that we have to percent escaping the +
characters, but advises that we do it manually:
var url = URLComponents(string: "https://www.wolframalpha.com/input/")!url.queryItems = [ URLQueryItem(name: "i", value: "1+2")]url.percentEncodedQuery = url.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
This is an inelegant work-around, but it works, and is what Apple advises if your queries may include a +
character and you have a server that interprets them as spaces.
So, combining that with your sendRequest
routine, you end up with something like:
func sendRequest(_ url: String, parameters: [String: String], completion: @escaping ([String: Any]?, Error?) -> Void) { var components = URLComponents(string: url)! components.queryItems = parameters.map { (key, value) in URLQueryItem(name: key, value: value) } components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B") let request = URLRequest(url: components.url!) let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, // is there data let response = response as? HTTPURLResponse, // is there HTTP response 200 ..< 300 ~= response.statusCode, // is statusCode 2XX error == nil // was there no error else { completion(nil, error) return } let responseObject = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] completion(responseObject, nil) } task.resume()}
And you'd call it like:
sendRequest("someurl", parameters: ["foo": "bar"]) { responseObject, error in guard let responseObject = responseObject, error == nil else { print(error ?? "Unknown error") return } // use `responseObject` here}
Personally, I'd use JSONDecoder
nowadays and return a custom struct
rather than a dictionary, but that's not really relevant here. Hopefully this illustrates the basic idea of how to percent encode the parameters into the URL of a GET request.
See previous revision of this answer for Swift 2 and manual percent escaping renditions.
Use NSURLComponents to build your NSURLlike this
var urlComponents = NSURLComponents(string: "https://www.google.de/maps/")!urlComponents.queryItems = [ NSURLQueryItem(name: "q", value: String(51.500833)+","+String(-0.141944)), NSURLQueryItem(name: "z", value: String(6))]urlComponents.URL // returns https://www.google.de/maps/?q=51.500833,-0.141944&z=6
font: https://www.ralfebert.de/snippets/ios/encoding-nsurl-get-parameters/
I am using this, try it in playground. Define the base urls as Struct in Constants
struct Constants { struct APIDetails { static let APIScheme = "https" static let APIHost = "restcountries.eu" static let APIPath = "/rest/v1/alpha/" }}private func createURLFromParameters(parameters: [String:Any], pathparam: String?) -> URL { var components = URLComponents() components.scheme = Constants.APIDetails.APIScheme components.host = Constants.APIDetails.APIHost components.path = Constants.APIDetails.APIPath if let paramPath = pathparam { components.path = Constants.APIDetails.APIPath + "\(paramPath)" } if !parameters.isEmpty { components.queryItems = [URLQueryItem]() for (key, value) in parameters { let queryItem = URLQueryItem(name: key, value: "\(value)") components.queryItems!.append(queryItem) } } return components.url!}let url = createURLFromParameters(parameters: ["fullText" : "true"], pathparam: "IN")//Result url= https://restcountries.eu/rest/v1/alpha/IN?fullText=true