Search code examples
swiftnsurlrequest

How to get the response to post string for eg in sign up i m facing error


I m unable to signup the and i m no getting the response when i m trying to print it on on console so here is the code foe sign up form and i m unable to sign up now:

    var request = URLRequest(url: URL(string: "http://fundedindia.org/funded/Doner_Registration.php")!)
    request.httpMethod = "POST"
    let postString = "Email=\(textfieldemail.text!)&Password=\(textfieldpassword.text!)&MobileNo=\(textfieldmobile.text!)&UserType=\(butname)&LoginType=Normal"
    request.httpBody = postString.data(using: .utf8)
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {                                                 // check for fundamental networking error
            print("error=\(String(describing: error))")
            return
        }

        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
            print("statusCode should be 200, but is \(httpStatus.statusCode)")
            print("response = \(String(describing: response))")

        }

        let json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
        let msg = json.value(forKey: "message") as! NSString!

        self.activityIndi.stopAnimating()
        self.activityIndi.hidesWhenStopped = true
        let alert : UIAlertView = UIAlertView(title: "Alert box!", message: "\(msg!).",delegate: nil, cancelButtonTitle: "OK")
        alert.show()
    }
    task.resume()
}

Solution

  • I tried to run your code with sample input

    var request = URLRequest(url: URL(string: "http://fundedindia.org/funded/Doner_Registration.php")!)
        request.httpMethod = "POST"
        let email: String = "pardeep@yopmail.com"
        let password: String = "12345678"
        let mobileNo: String = "9599904029"
        let butname: String = "1"
        let postString = "Email=\(email)&Password=\(password)&MobileNo=\(mobileNo)&UserType=\(butname)&LoginType=Normal"
        request.httpBody = postString.data(using: .utf8)
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {                                                 // check for fundamental networking error
                print("error=\(String(describing: error))")
                return
            }
    
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(String(describing: response))")
    
            }
    
            let json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
            let msg = json.value(forKey: "message") as! NSString!
    
            let alert : UIAlertView = UIAlertView(title: "Alert box!", message: "\(msg!).",delegate: nil, cancelButtonTitle: "OK")
            alert.show()
        }
        task.resume()
    

    Its responce status is not correct, please verify inputs and URl with your backend developer.

    response = Optional(<NSHTTPURLResponse: 0x604000425fe0> { URL: http://fundedindia.org/funded/Doner_Registration.php } { status code: 500, headers {
    "Cache-Control" = private;
    "Content-Length" = 5759;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Thu, 07 Dec 2017 08:58:40 GMT";
    Server = "Microsoft-IIS/8.0";
    "X-Powered-By" = "ASP.NET";
    "X-Powered-By-Plesk" = PleskWin;
    

    } })

    "status code: 500" should be 200 for success.