IOS Swift POST help needed [SOLVED]

I’ve been trying to perform a POST request in swift and i’ve hit a roadblock. I am primarily working off of the swift project that performs a GET here:

http://d.hatena.ne.jp/higepon/20140915/1410784693#c

i’m trying to modify the NSURL to add in the params line, but it docent like the “?” used relier in the string to separate the URL to the access token. the URL is:

let url = NSURL(string: "https://api.spark.io/v1/devices/my_core_ID_here/digitalwrite?access_token=my_access_token_here?params=D0,HIGH")

making the entire function:

private func pin0h() {

let url = NSURL(string: "https://api.spark.io/v1/devices/my_core_ID_here/digitalwrite?access_token=my_access_token_here?params=D0,HIGH")

let request = NSURLRequest(URL: url)

println(request)

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
if error != nil {
println("Request Error \(error.localizedDescription)")
return;
}
var err: NSError?
// parse the API response
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if err != nil {
println("JSON Error \(err!.localizedDescription)")
}
println(jsonResult)


}

}

I am getting an error with the access token:

code = 400;
error = "invalid_grant";
"error_description" = "The access token provided is invalid.";

if i remove ?params=D0,HIGH i get:

error = "Variable not found";
ok = 0;

wich indicates me token is just fine. if anyone has any suggestions I would be super grateful.

Solved:

swift code for POST

good stuff @jbrodie, sorry none of us got to it before you did :slight_smile:

Thanks for sharing, code. I’m also working on Spark and Swift iOS now.