[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write

App registers the user to the server but still gives an error. When I press the register button, the user registers, but the application crashes. After the application crashes, xcode displays the following screen.

It shows the error is here

Full code here

 func Register(post: NewUserModel, completion: @escaping (Result<Data, Error>) -> (Void)) {

        let parametrs: [String: Any] = [
            "ad": post.ad,
            "soyad": post.soyad,
            "email": post.email,
            "phone": post.phone,
            "dogumyili": post.dogumyili,
            "ulke": post.ulke,
            "sehir": post.sehir,
            "ilce": post.ilce,
            "adres": post.adres,
            "sifre": post.sifre,
            "resifre": post.resifre
        ]
        guard let url = URL(string: "http://ec2-3-74-42-117.eu-central-1.compute.amazonaws.com:8080/dentistodayapis/insertkullanici") else { return }

        var request = URLRequest(url: url)
        request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
        request.httpMethod = "POST"
        request.httpBody = try! JSONSerialization.data(withJSONObject: parametrs)

        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard
                let data = data,
                let response = response as? HTTPURLResponse,
                error == nil
            else {
                print("error", error ?? URLError(.badServerResponse))
                return
            }

            print(String(data: data, encoding: .utf8) as Any)
            guard (200 ... 299) ~= response.statusCode else {
                print("statusCode should be 2xx, but is \(response.statusCode)")
                print("response = \(response)")
                return
            }
            do {
                let response = try JSONSerialization.data(withJSONObject: data, options: .fragmentsAllowed)
                completion(.success(response))

            } catch {
                completion(.failure(error))
            }

        }
        task.resume()
    }

How can I get rid of the app crashing?

Answered by Scorean in 719029022

By response, I mean, for example, for if I request a user's info, then the server should give me a response of the user's info or a response of why the server refuses/fail to get the user's info. So you need to know what response the server gives when the registry is complete. And then you can parse it into an object that we can use in the app using JSONDecoder. (e.g. parse from data to a user's info. Data -> UserInfo)

Accepted Answer

By response, I mean, for example, for if I request a user's info, then the server should give me a response of the user's info or a response of why the server refuses/fail to get the user's info. So you need to know what response the server gives when the registry is complete. And then you can parse it into an object that we can use in the app using JSONDecoder. (e.g. parse from data to a user's info. Data -> UserInfo)

[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write
 
 
Q