0

trying to send data to server but every time i run my code an empty row is added to the database and an error appears in xcode as : "Invalid value around character 1." UserInfo={NSDebugDescription=Invalid value around character 1.}

 @IBAction func submitButton(_ sender: Any) {
    let parameters = ["paramname": "abc", 
               "paramemail": "hammadnaeem680@gmail.com",
               "parammobile": "1234567890", 
               "paramroomno": "402",
               "paramtime": "9",
               "paramsno": "137865",
               "paramcomplaint": "ios appp check",
               "paramblock": "CSE", 
                "paramtype": "server"] as Dictionary< String, String>
 guard let url=URL(string:"http://uietcloud.puchd.ac.in/mail/submit.php") 
   else {
            return
        }
        let session = URLSession.shared

        var request = URLRequest(url: url)
        request.httpMethod = "POST"
      request.setValue("Value", forHTTPHeaderField: "Key")

        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else {
            return}

         request.httpBody = httpBody

        request.addValue("application/json", forHTTPHeaderField: "Accept")
        session.dataTask(with: request) { (data, response, error) in    
         if let data = data
            {
                do
                {
                    let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
                    print(json)
                    print("not an error")
                } 
                catch{
                    print(error)
                    print("this is an error")
                      }

             }
        }
        .resume()
        }
hammad119
  • 1
  • 1

1 Answers1

0

Try turning off prettyPrinted option when serializing to JSON. Your server likely wants it ugly. :)

Smartcat
  • 2,834
  • 1
  • 13
  • 25
  • still not working, i updated the line to : guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return} – hammad119 Aug 13 '17 at 08:42