Welcome Guest, Not a member yet? Register   Sign In
$_POST is empty when using postman
#1

Hi Guys,

I am in the process of building a web application.  Using CI4 for the api, and react js front end.  

When posting data with react to my api, I am receiving the data properly.  

However, when I try to post in postman the $_POST, $_GET variable are all empty.  I've tried using POST with raw json data, x-www-form-urlencoded, form-data.  All of it comes in empty.  The only one that seems to work is when I send as a query string using GET. 

Has anyone encountered this issue before?

htaccess issue?

I've been stuck for 2 days now.  Not sure where to start.  Any pointers is appreciated. 

Thanks.
Reply
#2

It should be working. Can you post your controller and maybe a screenshot of your request in postman?
Have you tried using the IncomingRequest class ?

PHP Code:
$this->request->getPost() 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(12-30-2020, 03:04 PM)includebeer Wrote: It should be working. Can you post your controller and maybe a screenshot of your request in postman?
Have you tried using the IncomingRequest class ?

PHP Code:
$this->request->getPost() 

Here's my controller for users

This is Users.php controller
PHP Code:
<?php namespace App\Controllers\V1;

use 
CodeIgniter\RESTful\ResourceController;
use 
App\Models\V1\UserModel;
use 
App\Models\V1\Secure;

public function 
authorize_login()
    {
        try{
            set_error_handler( array( $this'handle_user_errors' ) );
            //retrieve the posted parameters
            $params $this->security->parse_incoming_data(false);

            //make sure we have an inflated params array and it is not empty
            if( is_array$params ) AND !empty( $params ) ):
                //instance the user model
                $user_model = new UserModel();
                //instance the user entity
                $user = new \App\Entities\Core\User();
                //fill the user entity with data
                $user->fill$params );
                //check to see if this email is already used
                $existing_records $user_model->where'user_email'$params['user_email'] )->findAll();
                //check if we have at least 1 record
                if( count$existing_records ) > ):
                    //validate that the passwords match
                    $password_verify password_verify$params['user_pass'], $existing_records[0]->user_pass );
                    if( $password_verify == true ):
                        //return user details
                        return json_encode(
                            array(
                                "id" => $existing_records[0]->id,
                                "user_email" => $existing_records[0]->user_email,
                                "user_fname" => $existing_records[0]->user_fname,
                                "user_lname" => $existing_records[0]->user_lname,
                            )
                        );
                    else:
                        throw new \Exception(EXCEPTIONPASSINCORRECT);
                    endif;
                else:
                    throw new \Exception(EXCEPTIONNOEMAILFOUND);
                endif;
            else:
                throw new \Exception(EXCEPTIONREQUIREDPARAMETERSMISSING);
            endif;
        }
        catch (\Exception $e)
        {
            //something nasty happened. raise the flags
            $this->response->setStatusCode400 );
            $response = array(
                "error" => $e->getMessage()
            );
            return json_encode$response );
        }
    

This is Secure.php model

PHP Code:
<?php
    
namespace App\Models\V1;
    
    
class Secure extends \CodeIgniter\Model
    
{

        public function decrypt_external$payload )
        {
            //check if payload data is an encrypted string
            if( !is_string$payload ) ):
                throw new \Exception('Payload data is not of type string');
            endif;
            //check if payload data is empty
            if( empty( $payload ) ):
                throw new \Exception('Payload data is empty');
            endif;
            //retrieve our IV length based on security method
            $iv_length openssl_cipher_iv_lengthSECURITY_CIPHER_METHOD );
            //separate our IV and Data
            list( $iv_salt$encrypted_data ) = explode"::"substr_replacebase64_decode$payload ), "::"$iv_length) );
            //let the decryption begin
            $decrypted_data openssl_decrypt$encrypted_dataSECURITY_CIPHER_METHODSECURITY_CIPHER_KEYtruemd5$iv_salttrue) );
            //check if decryption succeeded
            if( $decrypted_data !== false ):
                //data is still serialized. Lets unserialize it
                $unserialized_data unserialize$decrypted_data );
                return $unserialized_data;
            else:
                throw new \Exception('Error decrypting payload data');
            endif;
        }
        
        
public function parse_incoming_data$internal_request true )
        {
            //initialize our incoming request service
            $request service'request' );
            //GET the json response
            $json_array = (array) $request->getJSON();
            //GET the post response
            $post $request->getPost();
            //GET the get response
            $get $request->getGet();
            //validate which response is populated.
            if( is_array$json_array ) and array_key_exists"data"$json_array ) ):
                $data $json_array['data'];
            elseif( is_array$post ) and array_key_exists"data"$post ) ):
                $data $post['data'];
            elseif( is_array$get ) and array_key_exists"data"$get ) ):
                $data $get['data'];
            else:
                throw new \Exception('Required parameter [data] is missing');
            endif;
            //decrypt the payload
            if( $internal_request == true ):
                $decrypted_data $this->decrypt_internal$data );
            else:
                $decrypted_data $this->decrypt_external$data );
            endif;
            //scrub the data
            if( is_array$decrypted_data ) == true AND !empty( $decrypted_data ) ):
                //lets go through each value and clean it up
                foreach( $decrypted_data as $key => $value ):
                    if( $key == "user_pass" ):
                        $cleaned_data$key ] = password_hashstrip_tagstrim$value ) ), PASSWORD_DEFAULT );
                    else:
                        $cleaned_data$key ] = htmlentitiesstrip_tagstrim$value ) ) );
                    endif;
                endforeach;
            else:
                throw new \Exception('Invalid payload type, expected array');
            endif;

            return $cleaned_data;
        }
        
    


So as I've stated before.  When I submit to this function from the website. It works completed fine as intended.  When I post from postman, the payload is empty.
Reply
#4

There must be something wrong with your postman request. When you say the payload is empty, do you mean your $data or $post variable is empty? Also, why don’t you just call getPost(‘data’) instead of getting all the $_POST array then checking if ‘data’ is set... the getPost() function already does all this for you. It will return NULL if ‘data’ is not set. You can also call getPostGet() and it will look in $_POST first, then in $_GET.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

(01-01-2021, 08:41 AM)includebeer Wrote: There must be something wrong with your postman request. When you say the payload is empty, do you mean your $data or $post variable is empty? Also, why don’t you just call getPost(‘data’) instead of getting all the $_POST array then checking if ‘data’ is set... the getPost() function already does all this for you. It will return NULL if ‘data’ is not set. You can also call getPostGet() and it will look in $_POST first, then in $_GET.
If you do var_dump( $_POST ) inside my controller, then POST with postman, it will be an empty array(), but if you do it via the website, it works perfectly fine
Reply
#6

I figured it out. In postman, there was a setting...

Follow original HTTP Method
Redirect with the original HTTP method instead of the default behavior of redirecting with GET.

I turned it on, and everything worked perfectly.
Thanks for the help. Hopefully this thread can help someone else.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB