I am using a Combine URLSession to pull data from the Food Data Central API and the Data I am receiving is not being decoded with the JSON decoder. I know the Data is being received because I use the String(data:encoding: .utf8) as a debug print and I can see the downloaded data correctly in the console. I get an error message after the .decode completion failure that says "The data couldn't be read because it isn't in the correct format."
I am guessing I have to add something like the "encoder: utf8" statement in the .decode function. Or maybe transform the data in the .tryMap closure before returning. But I have searched the documentation and other sources and have not found anywhere that discusses this.
I am a fairly new to Swift (my first real app), I am hoping someone more-experienced can point me in the right direction. My code is as follows:
private func fdcSearch(searchFor searchText: String) {
let query = "https://api.nal.usda.gov/fdc/v1/foods/search?api_key=***&query=+Apple%20+Fuji"
let searchURL = "https://api.nal.usda.gov/fdc/v1/foods/search?"
let searchQuery = "&query="+searchString
print(searchURL+devData.apiKey+searchQuery)
// guard let url = URL(string: "https://api.nal.usda.gov/fdc/v1/foods/search?api_key=***&query=AppleFuji") else {
guard let url = URL(string: searchURL+devData.apiKey+searchQuery) else {
print("Guard error on url assignment") // debug statement
return
}
print("In fdcSearch") // debug statement
fdcSearchSubscription = URLSession.shared.dataTaskPublisher(for: url)
.subscribe(on: DispatchQueue.global(qos: .default))
.tryMap { (output) -> Data in
guard let response = output.response as? HTTPURLResponse, response.statusCode >= 200 && response.statusCode < 300 else {
print("bad server response") // debug statement
throw URLError(.badServerResponse)
}
print("got output") // debug statement
if let dataString = String(data: output.data, encoding: .utf8) { // debug statement
print("got dataString: \n\(dataString)") // debug statement
} // debug statement
return output.data
}
.receive(on: DispatchQueue.main)
.decode(type: [FDCFoodItem].self, decoder: JSONDecoder())
.sink { (completion) in
switch completion {
case .finished:
print("Completion finished") // debug statement
break
case .failure(let error):
print("Completion failed") // debug statement
print(error.localizedDescription)
}
} receiveValue: { [weak self] (returnedFoods) in
self?.foods = returnedFoods
print("returnedFoods: \(returnedFoods)") // debug statement
print("self?.foods: \(String(describing: self?.foods))") // debug statement
}
}
Any suggestions on how to handle this?