0

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
        )
    }
  }
}
udi
  • 3,672
  • 2
  • 12
  • 33
Muhammad Farooq
  • 263
  • 3
  • 10
  • can you post what is `Category` and `Json `? – Omer Tekbiyik Jun 07 '22 at 12:53
  • Could your print: `print("Data stringified: \(String(data: response.data ?? Date(), encoding: .utf8))")` and show us the output? – Larme Jun 07 '22 at 13:01
  • Since it's "somtimes", and I guess the URL doesn't change, I'd guess you have a rate limit per minutes/seconds, explaining the error, or server issue. It wouldn't suprise me to see "HTML" code errors... – Larme Jun 07 '22 at 13:04
  • Model: struct Category: Identifiable, Decodable { let id: Int let name: String let thumbnail: String let color: String } JSON Response: [ { "id": 6, "name": "Architecture", "color": "#000000", "thumbnail": "http://logomaker.one/public/files/upload/logo-files/categories/logo_cat_NAFaCi_Icon.jpg", "is_active": 1, "is_delete": 0, "created_at": "2021-12-30T02:49:49.000000Z", "updated_at": "2021-12-30T02:49:49.000000Z" } ] – Muhammad Farooq Jun 07 '22 at 13:58
  • @Larme I printed the response. [You can check from](https://docs.google.com/document/d/1W3APsaE6Wv0fIk0Mh6SH-G9ZYefbBA8CMxPrxLFckSs/edit?usp=sharing). I don't think it is from server side issue. I tried to send unlimited request using postman. It is not showing error in the postman. – Muhammad Farooq Jun 07 '22 at 14:11
  • Is that the print when it failed? There is no content on the given link. – Larme Jun 07 '22 at 14:13
  • I suggest you add a `debugPrint(response)` to your response handler to see what the response is actually receiving. – Jon Shier Jun 08 '22 at 03:49
  • I am getting this error "JSON could not be serialized because of error:\nThe data couldn’t be read because it isn’t in the correct format" but the app shows this error second time. First time the data load successful. – Muhammad Farooq Jun 08 '22 at 06:20
  • When it fails, print the content of the response then? Maybe as hex data: `response.data` See https://stackoverflow.com/questions/39075043/how-to-convert-data-to-hex-string-in-swift – Larme Jun 08 '22 at 22:30

0 Answers0