I have an app that controls some IoT devices. I am trying to make an older iHome outlet work that uses the evrythng API. It would need to take a command like this:
curl -X PUT \
https://api.evrythng.com/thngs/<DEVICE_ID>/properties/targetpowerstate1 \
-H 'authorization: MY_API_KEY' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '[{"value":"1"}]'
The issue is that the [{"value":"1"}] does not work trying to send as a paramter using alamofire due to the curly braces within the square braces. So I tried using NSMutableURLRequest doing something like:
let url = URL(string: "https://api.evrythng.com/thngs/\(self.id)/properties/targetpowerstate1")
let request = NSMutableURLRequest(url: url!)
let session = URLSession.shared
let stringParams = "[{\"value\":\"0\"}]"
request.httpBody = stringParams.data(using: String.Encoding.utf8)
request.httpMethod = "PUT"
request.addValue("no-cache", forHTTPHeaderField: "cache-control")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue(apikey, forHTTPHeaderField: "Authorization")
let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
guard error == nil else {
getvcfunc.statusMsg(error! as! String)
return
}
guard let data = data else {
getvcfunc.statusMsg("Data is empty")
return
}
let outputStr = String(data: data, encoding: String.Encoding.utf8)
getvcfunc.statusMsg(outputStr ?? "no value")
})
task.resume()
Please help me, I have been trying different things for hours now just to figure out how to send this PUT request with the curly braces.
- Dave