How do I get the data that is returned from a rest API call to Raspberry Pi's sensor?
I have been using postman to manipulate the GET and POST calls so I can see that it works and see the response. The information returned in the xcode console is as follows:
Optional( { URL: >http://192.168.1.83:8000/GPIO/10/value } { status code: 200, headers { "Cache-Control" = "no-cache"; "Content-Length" = 1; "Content-Type" = "text/plain"; Date = "Mon, 29 Feb 2016 23:40:32 GMT"; Server = "WebIOPi/0.7.1/Python3.4"; } })
The code I am using is generated from postman and is as follows:
import Foundation
let headers = [
"authorization": "Basic ***SNIP***",
"cache-control": "no-cache",
"postman-token": "e-4d2-2------d---9d---c0bc72a44f7e"
]
var request = NSMutableURLRequest(URL: NSURL(string: "http://192.168.1.83:8000/GPIO/10/value")!,
cachePolicy: .UseProtocolCachePolicy,
timeoutInterval: 10.0)
request.HTTPMethod = "GET"
request.allHTTPHeaderFields = headers
let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? NSHTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
I want the value returned by the call to the GPIO pin 10, which will be either a 0 or a 1. If I try to get the value from the completion handlers response it returns a Nil, and pretty much anything else I try just crashes it.