title: Cocoa And Swift - I want to make a simple rest API call using Swift 4 and Cocoa for Mac OS Application
body: <p>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.</p>
  
  <p>Here is the code:</p>
  
  <pre><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 -&gt; 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()
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/p1xvr.png" rel="nofollow noreferrer">See the info.plist</a></p>