I am trying to fetch response using API but i am getting below error. Sometimes the API work and sometime it gave me this error. The API works fine in the Postman.
failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 1, column 0." UserInfo={NSDebugDescription=Invalid value around line 1, column 0., NSJSONSerializationErrorIndex=0}))))))
My Code:
class TemplateObserver: ObservableObject {
@Published var category = [Category]()
init(){
fetchCategories()
}
func fetchCategories() {
let request = AF.request(Constants.CATEGORIES, method: .get)
request.responseDecodable(of: [Category].self) { response in
print("Category Response: (response)")
guard let result = response.value else { return }
self.category = result
}
}
}
I am trying to create object in the app.Swift file:
@main
struct Logo_MakerApp: App {
@ObservedObject var templateObserver = TemplateObserver()
var body: some Scene {
WindowGroup {
ContentView(
templateObserver: templateObserver
)
}
}
}