Pix And Codes
Pix And Codes

Reputation: 31

swift POST request not sending data to server

From my login view in my iphone app I get users username and password which I want to send to the server. I have made simple script just to check if my function for sending POST request to the server works. I dont have any errors but it seems that my script gets empty POST varaibles. Here is my function for sending parameters to my php server script:

func postDataToURL() {

    let json = [ "username" : "vanja", "password" : "dinamo", "iphone" : "1" ]

    print (json)

    do {
            let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted)

            // create post request
            let url = NSURL(string: "http://www.pnc.hr/rfid/login.php")!
            let request = NSMutableURLRequest(URL: url)
            request.HTTPMethod = "POST"

            // insert json data to the request
            request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
            request.HTTPBody = jsonData
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.addValue("application/json", forHTTPHeaderField: "Accept")


            let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in
                if error != nil{
                    print("Error 55 -> \(error)")
                    return
                }

                do {
                    let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject]

                    print("Result 34 -> \(result)")

                } catch {
                    print("Error  43-> \(error)")
                }
            }

            task.resume()
        }
        catch {
            //handle error. Probably return or mark function as throws
            print(error)
            return
        }
}

Here is my php script:

<?php 
if (isset($_POST['username']))
    $username = $_POST['username'];
else $username = "nista";

if (isset($_POST['password']))
    $password = $_POST['password'];
else $password = "nista";

if (isset($_POST['iphone']))
    $iphone = $_POST['iphone'];
else $iphone = "nista";

$arr = array('username' => $username, 'password' => $password, 'loggedIn' => 1, 'token' => 'ldsakj832WE32', 'iphone' => $iphone );
echo json_encode($arr);

?>

The problem is that script does not get any of my POST variables.

Upvotes: 0

Views: 889

Answers (1)

Pix And Codes
Pix And Codes

Reputation: 31

The problem is: from Swift I have sent json file, not POST varaibles. So I dont get variables form php with $_POST. I need to get the json file and encode it like this:

$json = file_get_contents('php://input');
//echo $json shoulkd show the json string

$array = json_decode($json, true);
// var_dump($arr) should show the array structure

$username = $array['username'];
$password = $array['password'];
$iphone = $array['iphone'];

Upvotes: 1

Related Questions