0

I have an iphone serial number "F17TN84HHG7F", I want to use swift code to auto enter this website field: https://d3velopersteam.com/check

enter image description here

and print the result received in console as: Model...Devide...Imei...enter code here:

enter image description here

I tried this code but the return result is: OOps not good JSON formatted response

let session = URLSession.shared
        let url = "https://d3velopersteam.com/check"
        let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        var params :[String: Any]?
        params = ["" : "F17TN84HHG7F"]
        do{
            request.httpBody = try JSONSerialization.data(withJSONObject: params as Any, options: JSONSerialization.WritingOptions())
            let task = session.dataTask(with: request as URLRequest as URLRequest, completionHandler: {(data, response, error) in
                if let response = response {
                    let nsHTTPResponse = response as! HTTPURLResponse
                    let statusCode = nsHTTPResponse.statusCode
                    print ("status code = \(statusCode)")
                }
                if let error = error {
                    print ("\(error)")
                }
                if let data = data {
                    do{
                        let jsonResponse = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
                        print ("data = \(jsonResponse)")
                    }catch _ {
                        print ("OOps not good JSON formatted response")
                    }
                }
            })
            task.resume()
        }catch _ {
            print ("Oops something happened buddy")
        }

hope everyone can help me rewrite!!

1 Answers1

0

could you try to use this code to debug your issue.

func showResponse(_ data: Data) {
    if let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers), let jsonData = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) {
        print("\n---> json: " + String(decoding: jsonData, as: UTF8.self))
    } else {
        print("=========> json data malformed")
    }
}

and use it here:

        if let data = data {
        showResponse(data)   // <--- here
            do{
                let jsonResponse = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
                print ("data = \(jsonResponse)")
            }catch _ {
                print ("OOps not good JSON formatted response")
            }
        }
            

and tell us what it prints (you can edit your question with this).