HTTP Request Syntax for Particle.publish

I’ve written a Swift 3 SDK for Particle that does nearly all I want, but I can’t get publish to work. I’ve looked at a bunch of sources, but I just can’t get the syntax right. I checked out the Particle official SDK, but got lost in all the AFNetworking code, and couldn’t actually find where they create the final request. This is what I’ve tried (along with numerous variations).

func publishEvent(name: String, data: String, isPrivate: Bool, ttl: Int, completionHandler: @escaping (_ error:Error?) -> Void) {
        let url = URL(string: "https://api.particle.io/devices/events")
        var postRequest = URLRequest(url: url!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10)
        postRequest.httpMethod = "POST"
        postRequest.setValue("Bearer \(accessToken!)", forHTTPHeaderField: "Authorization")
        let privateString = isPrivate ? "true" : "false"
        let bodyData = "name=\(name)&data=\(data)&private=\(privateString)&ttl=\(ttl)"
        postRequest.httpBody = bodyData.data(using: String.Encoding.utf8)
        
        session = URLSession.shared
        
        let dataTask = session.dataTask(with: postRequest) {data, response, taskError in
            print(response!)
            if (taskError == nil) {
                do {
                    if let infoDict = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as? [String:AnyObject] {
                        print("infoDict is: \(infoDict)")
                        //completionHandler(infoDict["error"] as! Error?)
                    }
                    
                } catch let jsonError as NSError {
                    print(jsonError.localizedDescription)
                }
            }else{
                print("taskError from executeFunction func is: \(taskError!.localizedDescription)")
            }
        }
        dataTask.resume()
    }

The result I get is,

[“ok”: 0, “error”: Not Found]

https://docs.particle.io/reference/api/#publish-an-event

should be https://api.particle.io/v1/devices/events

1 Like

Yeah, I tried that (it wasn’t clear from the docs I saw which to use). I still get the same result. I updated my original post to correct that mistake.

I also tried this variation that has the access token in the data (like the reference you linked) instead of in a header.

func publishEvent(name: String, data: String, isPrivate: Bool, ttl: Int, completionHandler: @escaping (_ error:Error?) -> Void) {
        let url = URL(string: "https://api.particle.io/devices/events")
        var postRequest = URLRequest(url: url!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10)
        postRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") // tried with and without this line
        postRequest.httpMethod = "POST"
        let privateString = isPrivate ? "true" : "false"
        let bodyData = "name=\(name)&data=\(data)&private=\(privateString)&ttl=\(ttl)&access_token=\(accessToken!)"
        postRequest.httpBody = bodyData.data(using: String.Encoding.utf8)
       ...
}

@Ric, I can see you added the devices part, but you are still missing the v1 part.

1 Like

Doh! I can’t believe I didn’t see that (myself, or in Kenneth’s post). I must have spent 5 hours on this yesterday. I’m sure I had it in there at one point, because I copied and pasted the code from another function that did have the v1. I do have an appointment with an eye doctor later this month; maybe that should have been sooner :flushed:

1 Like