If you wanna test Postman.. You can test on Postman. I couldn't decode data. How can I decode ?
Error:
keyNotFound(CodingKeys(stringValue: "data", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "data", intValue: nil) ("data").", underlyingError: nil))
Model:
// MARK: - CountryResponse
struct CountryResponse: Codable {
let countryData: [CountryData]
enum CodingKeys: String, CodingKey {
case countryData = "data"
}
}
// MARK: - CountryData
struct CountryData: Codable {
let code: String
let currencyCodes: [String]
let name, wikiDataID: String
enum CodingKeys: String, CodingKey {
case code, currencyCodes, name
case wikiDataID = "wikiDataId"
}
}
Service:
class CountryService {
func getAllCountry() {
if let url = URL(string: "https://wft-geo-db.p.rapidapi.com/v1/geo/countries?limit=10") {
var request = URLRequest(url: url)
request.addValue("wft-geo-db.p.rapidapi.com", forHTTPHeaderField: "x-rapidapi-host")
request.addValue("api key", forHTTPHeaderField: "x-rapidapi-key")
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
do {
let response = try JSONDecoder().decode(CountryResponse.self, from: data)
print("response: \(response)")
} catch let error {
print("data decode edilemedi. \(error)")
}
}
task.resume()
} else {
print("hatalı url.")
}
}
}