Sending JSON Data with Xcode : I'm building a chat app and I understand how to receive the data back but I'm having trouble sending the data. Im trying to take two UITextField values which are the username and the message and send the data.

Variables

    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var textBox: UITextField!

Request To Receive

    @IBAction func sendMessage(_ sender: UIButton) {
        let parameters = ["user": username.text, "message": textBox.text] as! [String: String]
        
        //create the url with NSURL
        let url = NSURL(string: "http://website.com/getChatLogJSON.php")
        
        //create the session object
        let session = URLSession.shared
        
        //now create the NSMutableRequest object using the url object
        let request = NSMutableURLRequest(url: url! as URL)
        request.httpMethod = "POST" //set http method as POST
        
        do {
            request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
            
        } catch let error {
            print(error.localizedDescription)
        }
        
        //HTTP Headers
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        
        //create dataTask using the session object to send data to the server
        let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
            
            guard error == nil else {
                return
            }
            
            guard let data = data else {
                return
            }
            
            do {
                //create json object from data
                if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: AnyObject] {
                    print(json)
                    // handle json...
                }
                
            } catch let error {
                print(error.localizedDescription)
            }
            
        })
        
        task.resume()

    }