By MrSSS16


2014-12-31 16:48:46 8 Comments

I'm very new to swift, so I will probably have a lot of faults in my code but what I'm trying to achieve is send a GET request to a localhost server with paramters. More so I'm trying to achieve it given my function take two parameters baseURL:string,params:NSDictionary. I am not sure how to combine those two into the actual URLRequest ? Here is what I have tried so far

    func sendRequest(url:String,params:NSDictionary){
       let urls: NSURL! = NSURL(string:url)
       var request = NSMutableURLRequest(URL:urls)
       request.HTTPMethod = "GET"
       var data:NSData! =  NSKeyedArchiver.archivedDataWithRootObject(params)
       request.HTTPBody = data
       println(request)
       var session = NSURLSession.sharedSession()
       var task = session.dataTaskWithRequest(request, completionHandler:loadedData)
       task.resume()

    }

}

func loadedData(data:NSData!,response:NSURLResponse!,err:NSError!){
    if(err != nil){
        println(err?.description)
    }else{
        var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
        println(jsonResult)

    }

}

7 comments

@Rob 2014-12-31 17:57:54

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 else {                           // was there no error, otherwise ...
                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.

@MrSSS16 2015-01-01 00:26:33

Thanks for the fantastic answer. I just have one question,I am still a bit confused as to what the extension string is doing to the values ? Also when would I need to use HttpBody then?

@Rob 2015-01-01 10:02:26

The string extension is percent escaping the values per RFC 3986. There are certain characters that have special meanings in URLs (e.g. & separates one parameter from the next, so if & occurs in value, you cannot let it go by unescaped). Regarding HTTPBody, you should not use it in GET request; It is used in POST but not GET.

@jigzat 2015-03-01 18:21:22

This looks so simple, what are the advantages of using something like github.com/xyyc/SwiftSocket instead of this? I'm sorry I'm new to all this.

@Rob 2015-03-01 19:34:44

That might not be the right comparison, because that's a sockets library and this is HTTP. Closer is something like Alamofire, but that is (a) more flexible (can handle JSON requests, x-www-form-urlencoded requests, multipart, etc.); (b) more functional (e.g. handles authentication challenges); and (c) gets you out of the weeds of HTTP programming. If anything, my answer above is intended as a cautionary tale of the risks of just creating your own NSMutableURLRequest, pointing out that there's more to it than the OP suggests.

@Isuru 2015-03-18 12:54:40

This is a beautiful solution. Thanks @Rob. Btw is there any difference in supplying parameters like this NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: nil)? parameters being an array of dictionaries [String: String]

@Rob 2015-03-18 13:01:29

It doesn't make much sense to me to be using JSON with GET request. If you're sending information to a server, that's POST, not GET. And you'd specify a different Content-Type header. And you don't need any of this percent-encoding stuff. Bottom line, a lot of the execution details are different.

@Isuru 2015-03-18 13:05:19

@Rob I see. Thank you.

@Roi Mulia 2015-09-18 18:03:27

Rob . chance you can update the extension method to swfit 2?

@Eddie Sullivan 2016-06-13 10:51:12

Great answer. I think you can use NSCharacterSet.URLQueryAllowedCharacterSet() instead of manually creating the allowed characters.

@Rob 2016-06-13 14:02:06

@EddieSullivan - Not quite. If you do that, you have to make a mutable copy and then remove a few characters, notably & and +, which URLQueryAllowedCharacterSet will allow to pass unescaped. See stackoverflow.com/a/35912606/1271826.

@matt.writes.code 2016-10-12 20:17:33

@Rob this is a great answer. Could you update it for Swift 3 please?

@Rob 2016-10-12 20:19:03

Updated for Swift 3.

@RonLugge 2017-02-14 19:03:40

Just as a note, the dictionary category breaks if the dictionary contains non-string values such as Int data types.

@Rob 2017-02-14 19:37:59

Agreed, I was trying to keep it simple, and you might have rendition that handles a broader array of values. For example, this rendition renders dates, coordinates, booleans, strings, numeric values, etc.

@ADB 2019-07-31 11:42:52

@Rob this is brilliant - thank you! Everything makes sense except (200 ..< 300) ~= response.statusCode. What does that do please? And what is that operator called - I searched but couldn't find it in Swift. Apologies if that's a really obvious question!

@Rob 2019-07-31 14:21:19

It is the pattern-matching operator. In this case, it effectively says “does the range of 200 up to (and not including) 300 contain the value statusCode.”

@ADB 2019-07-31 20:18:00

Thanks @Rob - that's a great tip!

@Reza Shirazian 2017-06-12 00:41:20

You can extend your Dictionary to only provide stringFromHttpParameter if both key and value conform to CustomStringConvertable like this

extension Dictionary where Key : CustomStringConvertible, Value : CustomStringConvertible {
  func stringFromHttpParameters() -> String {
    var parametersString = ""
    for (key, value) in self {
      parametersString += key.description + "=" + value.description + "&"
    }
    return parametersString
  }
}

this is much cleaner and prevents accidental calls to stringFromHttpParameters on dictionaries that have no business calling that method

@anoop4real 2017-03-29 16:41:29

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

@Danut Pralea 2016-12-23 13:47:55

Swift 3:

extension URL {
    func getQueryItemValueForKey(key: String) -> String? {
        guard let components = NSURLComponents(url: self, resolvingAgainstBaseURL: false) else {
              return nil
        }

        guard let queryItems = components.queryItems else { return nil }
     return queryItems.filter {
                 $0.name.lowercased() == key.lowercased()
                 }.first?.value
    }
}

I used it to get the image name for UIImagePickerController in func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]):

var originalFilename = ""
if let url = info[UIImagePickerControllerReferenceURL] as? URL, let imageIdentifier = url.getQueryItemValueForKey(key: "id") {
    originalFilename = imageIdentifier + ".png"
    print("file name : \(originalFilename)")
}

@Ilias Karim 2020-04-19 20:09:47

your response doesn't answer OPs question

@etayluz 2016-11-16 02:05:21

This extension that @Rob suggested works for Swift 3.0.1

I wasn't able to compile the version he included in his post with Xcode 8.1 (8B62)

extension Dictionary {

    /// Build string representation of HTTP parameter dictionary of keys and objects
    ///
    /// :returns: String representation in the form of key1=value1&key2=value2 where the keys and values are percent escaped

    func stringFromHttpParameters() -> String {

        var parametersString = ""
        for (key, value) in self {
            if let key = key as? String,
               let value = value as? String {
                parametersString = parametersString + key + "=" + value + "&"
            }
        }
        parametersString = parametersString.substring(to: parametersString.index(before: parametersString.endIndex))
        return parametersString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
    }

}

@Ben-Hur Batista 2015-12-07 13:46:30

Use NSURLComponents to build your NSURL like 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/

@Jan ATAC 2016-03-08 13:35:53

In spite of Rob's impressive answer, yours is simpler and works.

@John Rogers 2016-06-01 04:10:13

This should be the accepted answer. It is recommended to use NSURLComponents along with query items to construct URLs. Much safer and less prone to error.

@Fabio Guerra 2016-07-22 15:10:32

I use:

let dictionary = ["method":"login_user",
                  "cel":mobile.text!
                  "password":password.text!] as  Dictionary<String,String>

for (key, value) in dictionary {
    data=data+"&"+key+"="+value
    }

request.HTTPBody = data.dataUsingEncoding(NSUTF8StringEncoding);

Related Questions

Sponsored Content

37 Answered Questions

[SOLVED] Split a String into an array in Swift?

17 Answered Questions

[SOLVED] #ifdef replacement in the Swift language

15 Answered Questions

[SOLVED] How to iterate a loop with index and element in Swift

17 Answered Questions

[SOLVED] How do I call Objective-C code from Swift?

  • 2014-06-02 20:05:42
  • David Mulder
  • 291399 View
  • 982 Score
  • 17 Answer
  • Tags:   objective-c swift

23 Answered Questions

[SOLVED] @selector() in Swift?

19 Answered Questions

[SOLVED] How do I make an HTTP request in Swift?

20 Answered Questions

[SOLVED] #pragma mark in Swift?

  • 2014-06-03 14:05:56
  • Arbitur
  • 224702 View
  • 941 Score
  • 20 Answer
  • Tags:   swift documentation

9 Answered Questions

[SOLVED] Swift Beta performance: sorting arrays

1 Answered Questions

[SOLVED] Swift downloading data from web

  • 2015-03-24 09:35:52
  • Stevik
  • 964 View
  • 5 Score
  • 1 Answer
  • Tags:   swift

1 Answered Questions

[SOLVED] how to use JsonArray out of Queue

Sponsored Content