Search code examples
iosswiftopenhab

Posting API request to Openhab2 and Swift


I am trying to post a request to my Openhab system from a custom Swift iOS application I am developing however I can't seem to get a response.

I'm using the below but can't get the sensor to trigger.

AF.request("http://192.168.1.1:8080/rest/items/BinarySensor", method: .post, parameters: ["":"ON"], encoding: URLEncoding.httpBody, headers: ["Content-Type":"text/plain"])

Any help appreciated.


Solution

  • You need to change your encoding to JSONEncoding.default and your headers to application/json. Here is how your request would look like now

    AF.request("http://192.168.1.1:8080/rest/items/BinarySensor", method: .post, parameters: ["":"ON"], encoding: JSONEncoding.default, headers: ["Content-Type":"application/json"])