Skip to content

Instantly share code, notes, and snippets.

@RealEmmettS
Last active January 7, 2022 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RealEmmettS/6653b772321215de7a5b117246dbe4ff to your computer and use it in GitHub Desktop.
Save RealEmmettS/6653b772321215de7a5b117246dbe4ff to your computer and use it in GitHub Desktop.
var api_result:[String]?
// The sample API request below is modeled off of the Gater API sample request.
// https://docs.gater.dev/the-basics/sending-and-receiving
// The default response to the sample API used below is
// {"main":{"status":"Gater is alive and well"}}
//MARK: API Request
func apiRequest(){
let url = URL(string: ("https://api.gater.dev/status") )!
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
let json = try? JSONSerialization.jsonObject(with: data, options: [])
//MARK: Parse API result
if let dictionary = json as? [String: Any] {
//print(dictionary)
if let rawResult = dictionary["main"]{
//result is type __NSSingleEntryDictionaryI
guard let dictResult = rawResult as? [String:String] else {return}
print(dictResult["status"]!)
}
}
}
task.resume()
}//end of apiRequest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment