Can anyone suggest me how to make a rest api call for Mac OS Application using Swift 4 And Cocoa. I tried lots of ways but unable to make the api call.
Here is the code:
func callAPI(){
let AuthorizationToken = "Basic mykey="
var request = URLRequest(url: URL(string: "http://myipaddress/api/Batch")!)
request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue(AuthorizationToken, forHTTPHeaderField: "Authorization")
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
//print(response!)
do {
let json = try JSONSerialization.jsonObject(with: data!)
print(json)
self.ApiValue.stringValue = "Success"
} catch {
print("error")
self.ApiValue.stringValue = "Error"
}
})
task.resume()
}