I'm facing this issue with POST in CodeIgniter which is not working while if I switch to GET, that works fine.
Login Controller
public function login_check(){
print_r($this->input->post());
if($this->input->post('email')!=NULL){
echo '1';
}
else{
header('Content-Type: application/json');
echo json_encode( array('a' => $this->input->post('email')));
}
CSRF is set to false in config file, while base url is set to http://localhost/xyz/
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Routes
$route['api/login-check'] = 'login/login_check';
If I set $this->input->get('email')
while setting method GET in postman, that works absolutely okay.
What's something that I'm missing? Any help in this will be appreciated.
EDIT:
Response from postman:
Array() {"a":null}
The code is doing exactly as you are asking it to do.
A break down of your code goes like...
If I get something from $this->input->post('email') then
echo '1';
else if $this->input->post('email') is NULL
then assign NULL to a and return it in a json_encoded array.
Going by your code it might meant to be something like...
public function login_check(){
print_r($this->input->post());
if($this->input->post('email') == NULL){ // This was !=
echo '1';
}
else{
header('Content-Type: application/json');
echo json_encode( array('a' => $this->input->post('email')));
}
The Only change being to alter != to == in your if statement.
One of those "stare at it too long and never see it" simple Logic Error :)
A better recommendation is to use something like...
public function login_check(){
print_r($this->input->post());
if($this->input->post('email')){
header('Content-Type: application/json');
echo json_encode( array('a' => $this->input->post('email')));
exit();
}
else {
// Handle the case where no email was entered.
echo '1';
}
}
That should get you back on track.
Update: I have tried this in postman (just installed it ) and for a POST you need to set the key/value under Body and not Headers as you do with GET, if that's something you were also missing.