Home > AI > IOS > SwiftUI >

Postman

Example:

struct Postman: Decodable, Hashable {
    let valid: Bool
}

enum MyError: Error {
    case invalidServerResponse
}



var components = URLComponents()
components.scheme = "https"
components.host = "postman-echo.com"
components.path = "/time/valid"
components.queryItems = [
    URLQueryItem(name: "timestamp", value: "2016-10-10"),
]

let url = components.url
print(url!)


let task = URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
    print("good")
    guard error == nil else {
        print("Error \(error!)")
        return
    }
    guard let data = data else {
        print("No data found")
        return
    }

    do {
        let postman = try JSONDecoder().decode(Postman.self, from: data)
           DispatchQueue.main.async {
              print("\(postman.valid)")
           }
        } catch let jsonError {
           print(jsonError)
        }
})

task.resume()

Leave a Reply