使用Parse查询在Swift中调用REST API

时间:2021-06-17 21:25:58

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).

我正在尝试在我使用内置REST API创建的Parse数据库上发出GET请求。当用户将文本输入UISearchBar时,将进行API调用,最终目标是在UITableView中显示返回的数据。下面的代码仅捕获我尝试发出有效的HTTP请求,我试图查看“Query1”是否与搜索字符串匹配(“Query1”是我的Parse数据库中的一个参数,基本上用作关联的搜索词)。

//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.

结果是无法构建的代码,因为我无法弄清楚如何使用Swift将参数应用于Parse REST API。任何帮助,将不胜感激。

1 个解决方案

#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.

我在其他地方得到了帮助,但是想要给出有同样问题的人的答案。下面是Swift中的一个Parse REST API调用示例,它使用了我在上面列出的相同参数。

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()
    }

#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.

我在其他地方得到了帮助,但是想要给出有同样问题的人的答案。下面是Swift中的一个Parse REST API调用示例,它使用了我在上面列出的相同参数。

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()
    }