Post

Replies

Boosts

Views

Activity

Swift.DecodingError.dataCorrupted and app crash with "The given data was not valid JSON.", underlyingError: Optional(Error Code=3840 "No value"
here is my code struct ServerMessage: Codable { let error, email, firstname, lastname: String let Id: Int } out of 5 variables returned from api, 4 are string and 1 is int, class HttpAuth: ObservableObject { var didChange = PassthroughSubject<HttpAuth, Never>() var authenticated = false { didSet { didChange.send(self) } } func checkDetails(useremail:String, userpass: String) { guard let url: URL = URL(string: "http://www.mytestdomain.com/webapi/Users.php") else { print("invalid login URL") return } var urlRequest: URLRequest = URLRequest(url: url) urlRequest.httpMethod = "POST" urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type") do { let params = ["action": "login", "UserEmail": useremail, "UserPass": userpass] urlRequest.httpBody = try JSONSerialization.data(withJSONObject: params, options: .init()) URLSession.shared.dataTask(with: urlRequest) { (data, resp, err) in guard let data = data else { return } let finalData = try! JSONDecoder().decode([ServerMessage].self, from: data) print(finalData) if let err = err { print("Failed to login: ", err) } print("Login Sucess") }.resume() } catch { print("Failed to get data", error) } } } and this is the output of my php based web api {"error":"false","Id":1,"email":"emailidatmaildotcom","firstname":"somename","lastname":"lastname"} i double checked the api using postman. i am getting this error 2021-07-29 19:44:00.378908+0530 kapp[4536:146582] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed app/login.swift:79: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}))) 2021-07-29 19:44:02.613900+0530 kapp[4536:145289] kapp/login.swift:79: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}))) (lldb) edit: once the app is crashed, decode is showing this error also Thread 12: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) the api is written using php, any help is appreciated, thanks.
2
1
3.9k
Aug ’21