2

I'm attempting to make a GET request on a Parse database I created using the built-in REST API. The API call is to be made when a user enters text into a UISearchBar, with the ultimate goal being to display the returned data in a UITableView. The code below only captures my attempt to make a valid HTTP request, where I am trying to see if "Query1" matches the search string ("Query1" is a parameter in my Parse database that essentially serves as an associated search term).

//Mark - UISearchBarDelegate

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    makeRequest(searchBar.text)
}

func makeRequest (searchString : String) {

    //REST API call to the sampleObjectData class
    var request = NSMutableURLRequest(URL: NSURL(string: "https://api.parse.com/1/classes/sampleObjectData")!)
    let session = NSURLSession.sharedSession()
    request.HTTPMethod = "GET"

    //THIS IS MY TROUBLE AREA
    var params = urllib.urlencode({"where";:json.dumps({
        "Query1": "\(searchString)"
        })})

    var error: NSError?

    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &error)
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    //The kAppId & kRestAPIKey calls are referencing contstants at the top of the file
    request.addValue("X-Parse-Application-Id", forHTTPHeaderField: kAppId)
    request.addValue("X-Parse-REST-API-Key", forHTTPHeaderField: kRestAPIKey)


    var task = session.dataTaskWithRequest(request, completionHandler: { (data, response, err) -> Void in
        var stringData = NSString(data: data, encoding: NSUTF8StringEncoding)
        println(stringData)
    })
    task.resume()
}

The result is code that will not build, as I cannot figure out how to apply parameters to the Parse REST API using Swift. Any help would be appreciated.

1 Answer 1

10

I received assistance elsewhere, but wanted to post the answer I was given for anyone that has the same issue. Below is a sample Parse REST API call in Swift that uses the same parameters I laid out above.

func makeParseRequest(searchString: String) {

        var request = NSMutableURLRequest()
        request.HTTPMethod = "GET"
        request.addValue(kAppId, forHTTPHeaderField: "X-Parse-Application-Id")
        request.addValue(kRestAPIKey, forHTTPHeaderField: "X-Parse-REST-API-Key")
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")

        var params = ["Query1" : "\(searchString)"]
        var error: NSError?
        var paramsJSON = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &error)
        var paramsJSONString = NSString(data: paramsJSON!, encoding: NSUTF8StringEncoding)
        var whereClause = paramsJSONString?.stringByAddingPercentEscapesUsingEncoding(NSASCIIStringEncoding)

        let urlString = "https://api.parse.com/1/classes/sampleObjectData"
        var requestURL = NSURL(string: String(format: "%@?%@%@", urlString, "where=", whereClause!))

        request.URL = requestURL!

        var task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, err) -> Void in
            var stringData = NSString(data: data, encoding: NSUTF8StringEncoding)
            println(stringData)
        })

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

1 Comment

In iOS 9, stringByAddingPercentEscapesUsingEncoding is deprecated. This works instead: stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

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.