$this->input->post() returns NULL in codeigniter rest api implementation

1.3k views Asked by At

In the file RestClient.php the lines $uname = $this->input->post('username') and $pwd = $this->input->post('password') returns NULL.

This is my (View) loginform.php

    <?php echo form_open('api/RestClient/PostCurl'); ?>
    <div class="form-group">
        <label for = "username"><h5>Username</h5>
            <input type = "text" name = "username" class="form-control" required
                   oninvalid = "this.setCustomValidity('username is required')"
                   oninput = "this.setCustomValidity('')" autocomplete="off"> <br>
        </label>
    </div>
    <div class="form-group">
        <label for = "password"><h5>Password</h5>
            <input type = "password" name = "password" class="form-control" required
                   oninvalid = "this.setCustomValidity('password is required')"
                   oninput = "this.setCustomValidity('')" autocomplete="off"> <br>
        </label>
    </div>
    <div class="form-group">
        <a href="<?php echo site_url('api/RestClient/PostCurl'); ?>"> <input id="loginsubmit" type = "submit" class="btn btn-success" class="form-control" value = "Login"/>
        </a>
    </div>

This is my RestServer API controller - LoginFormApi.php

    class LoginFormApi extends REST_Controller {

public function __construct() {
    parent::__construct();
    $this->load->helper('url');
    $this->load->library('form_validation');
    $this->load->model('LoginFormModel');
    $this->load->model('EmployeeAccountModel');
}

public function LoginForm_post() {
    $userName = $this->post('username');
    $password = $this->post('password');
    $error = 'Invalid' . $userName;
    if (!$userName || !$password) {
        $this->set_response($error, REST_Controller::HTTP_NOT_FOUND);
    }
    $id = $this->LoginFormModel->Validate($userName, $password);
    if ($id) {
        $token['id'] = $id;
        $token['username'] = $userName;
        $date = new DateTime();
        $token['iat'] = $date->getTimestamp();
        $token['exp'] = $date->getTimestamp() + 60 * 60 * 5;
        $output['id_token'] = JWT::encode($token, "~~~JWT Auth Key !!!!!!~~~");
        $this->set_response($output, REST_Controller::HTTP_OK);
    } else {
        $this->set_response($error, REST_Controller::HTTP_NOT_FOUND);
    }
}

This is my client request controller using cURL - RestClient.php the lines $uname = $this->input->post('username') and $pwd = $this->input->post('password') returns NULL.

    class RestClient extends CI_Controller{
public function __construct() {
    parent::__construct();
    $this->load->helper('url','form');
   $this->load->library('form_validation');
    $this->load->library('Curl');
}

public function PostCurl(){
    $username = 'admin';
    $password = 'apitest@1234';
    $uname = $this->input->post('username');
    $pwd = $this->input->post('password');
    echo '~'.$uname.$pwd.'~';
    $curl_handle = curl_init('http://localhost:8085/ci-api/index.php/api/LoginFormApi/LoginForm');
    curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    $dataPost = array('username' => $uname,'password' => $pwd);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $dataPost);
    curl_setopt($curl_handle, CURLOPT_USERPWD, $username.':'.$password);
    $data = curl_exec($curl_handle);
    curl_close($curl_handle);
    $result = json_decode($data);
    if(isset($result->status) && $result->status === 'success'){
        echo 'success' .$result;
    }else{
        $post = var_dump($dataPost);
        echo $post.'failure'.$result;
    }
}

}

1

There are 1 answers

0
Ankit Ghosh On BEST ANSWER

I tried the following js snippet in the view and it worked, the post data is getting accessed in the RestClient.php controller:

    <script>
        function FormData(){
            var formData = {};
            formData.username = $('#username').val();
            formData.password = $('#password').val();
            $.ajax({
               url: 'http://localhost:8085/ci-api/index.php/api/RestClient/PostCurl',
               type: 'post',
               dataType: 'json',
               data: JSON.stringify(formData),
               ContentType: 'application/json'
            });
        }
    </script>