1

Looking on how to make a http get call with query string in Swift2

I can see lots of examples of Objective-c and old versions of swift, but cant find for Swift2

1 Answer 1

2

Here is an example that performs a search on the iTunes API:

let timeout = 11 as NSTimeInterval
let searchTerm = "philip+glass"
let url = NSURL(string: "https://itunes.apple.com/search?term=\(searchTerm)")
let request: NSURLRequest = NSURLRequest(URL: url!,
                                        cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData,
                                        timeoutInterval: timeout)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task: NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {
    (data, response, error) in
        if response == nil {
            print("Timeout")
        } else {
            print(String(data: data!, encoding: NSUTF8StringEncoding))
        }
    }
)

task.resume()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but say i want to set multiple name value parameters and construct them with appropriate encoding, is there some function which construct those parameters easily?
The answer at stackoverflow.com/questions/27723912/… has some extensions that will accomplish those things for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.