I'm following a tutorial at the moment and trying to apply it to something that I can use and understand better then just a general random API and I ran into this issue when trying to Post data. "This data couldn't be read because it isn't in the correct format"
Using Xcode and Swift.
This is the API I'm trying to grab from is https://github.com/ToontownRewritten/api-doc/blob/master/invasions.md http://toontownrewritten.com/api/invasions
I put it formatted under the code snippet
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
fetchPostData { (posts) in
for post in posts {
print(post.type)
}
}
}
func fetchPostData(completionHandler: @escaping ([Post]) -> Void){
let url = URL(string: "https://www.toontownrewritten.com/api/invasions")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
let postsData = try JSONDecoder().decode([Post].self, from: data)
completionHandler(postsData)
} catch {
let error = error
print(error.localizedDescription)
}
}.resume()
}
}
Here is the class that I have for posting the data
import Foundation
struct Post: Codable {
var invasions: String!
var type: String!
var asOf: Int!
var progress: Int!
}
This is the API I'm trying to grab from formatted. http://toontownrewritten.com/api/invasions
{
"lastUpdated":1624089300,
"invasions":{
"Thwackville":{
"asOf":1624089291,
"type":"Big Wig",
"progress":"477/8000"
},
"Splashport":{
"asOf":1624089283,
"type":"Short Change",
"progress":"631/3214"
},
"Kaboom Cliffs":{
"asOf":1624089282,
"type":"Legal Eagle",
"progress":"609/4000"
},
"Fizzlefield":{
"asOf":1624089299,
"type":"Bean Counter",
"progress":"6141/8000"
},
"Boingbury":{
"asOf":1624089283,
"type":"Money Bags",
"progress":"2246/3211"
}
},
"error":null
}