Welcome Guest, Not a member yet? Register   Sign In
Capturing post data from C++ application in Codeigniter 4
#1

I am upgrading our site from Codeigniter 3 to Codeigniter 4. So far it has been tedious, but mostly gone well. I am running into an issue now with a controller that will not recognize posted values from an app written in C++. This app has been working with CI 3 for many years now, but... well, CI4. I don't know all the details about the C++ app, but the developer assures me he is definitely using HTTP_VERB_POST to send the data to the controller.

I have created a view for the controller and CAN successfully Post values from a form using my browser.

However - Posting from the app 1) tells me the request method is GET and 2) NO values at all have been passed. It DOES however - send me the response I would expect if no values are passed to the controller......

After adding the following to my controller:
PHP Code:
header('Access-Control-Allow-Origin: *');
        echo 
$this->request->getMethod();
        echo 
"\n POST VARS = ";
        
print_r($_POST);
        echo 
"\n GET VARS = ";
        
print_r($_GET);
        echo 
"\n"

I get the following result:
Code:
get
POST VARS = Array
(
)

GET VARS = Array
(
)

Activation: No
Error: No parameters sent.

The last two lines (Activation and Error) are what I would expect to get from this controller if, indeed, no parameters are sent.

Oh - and no, I do not have CSRF (except for default Cookie based CSRF Protection) enabled in my CI 4 application.

Any ideas would be greatly appreciated!
Reply
#2

Here is the line from the C++ app that calls the controller in question:
Code:
subscriptioninfo = pSession->OpenRequest(CHttpConnection::HTTP_VERB_POST, address, NULL, NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE);
Reply
#3

I’m not familiar with the C++ code but I’m with you: the incoming request is clearly GET method. You could try writing some unit tests or using Insomnia/Postman to verify your app’s functionality apart from the C++ client.

One suggestion: try capturing the entire Request to the logs to see if there are any other “gotchas” in there. Something like from your controller:

log_message('debug', print_r($this->request, true));
Reply
#4

One last thought… make sure your server is configured correctly. It would be unusual, but I could imagine some SSL forwarding or server proxy affecting the incoming HTTP method inadvertently.
Reply
#5

(This post was last modified: 05-16-2022, 01:00 AM by InsiteFX.)

Also do not use the access control * use the full url link. for security reasons.

Code:
Header add Access-Control-Allow-Origin: "https://yoursite.com"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header add Access-Control-Allow-Headers: "Upgrade-Insecure-Requests"
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 05-17-2022, 08:35 AM by junosapien. Edit Reason: addintional info )

Thanks for the input. I've tried a number of different things (including Postman, which works), and think the issue is that CI4 is not receiving the Content-Type header from the C++ app (it is explicitly set with this:
Code:
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded");

Here is a simple print of the headers received by both CI4 and CI3 on the same server. (I have copied my old CI3 app to a directory called 'dev' inside the Public directory of CI4.)

Headers from CI3 in Public folder
host: test.site.com
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization: Basic XXXXXXXXXXXXXXXXXXXX
Content-Type: application/x-www-form-urlencoded
Cookie: ci_session=9do54stine1po2c0tren49gr17eq068g
User-Agent: CustomName
X-Forwarded-For: 96.68.149.126
X-Forwarded-Port: 443
X-Forwarded-Proto: https
Content-Length: 164
Connection: keep-alive

Response from CI3
Subscription number: 9999999999999999
Activation: No
Error: Activation Key does not exist.


Headers from CI4
host: test.site.com
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization: Basic XXXXXXXXXXXXXXXXXXXX
Cookie: ci_session=g6v3qlgms2bv3ijv19hpl3cgnvnbg4kk
User-Agent: CustomName
X-Forwarded-For: 96.68.149.126
X-Forwarded-Port: 443
X-Forwarded-Proto: https
Connection: keep-alive

Response from CI4
Subscription number:
Activation: No
Error: No parameters sent.

As you can see - the C++ app connects to the CI3 controller, with the Content-Type in the header and returns what I would expect given the parameters I passed in. However, CI4 is not receiving the Content-Type header and tells me that no parameters were passed.

Any thoughts as to why CI4 would be unable to recognize the Content-Type sent by the C++ app?
Reply
#7

OK - So I do not know if I have gone off on the right path or not, but while reading this post on Stackoverflow, https://stackoverflow.com/questions/4919...st-request, I noticed one of the comments on one of the answers:

“In my case, my form had action="mysite.com/somefolder and that caused the $_POST to be empty. But when I changed the action to mysite.com/somefolder/ it worked as expected.”

And then I see that in CI4 (as opposed to CI3) the URLs/URIs are indeed missing that trailing slash in the server variables ($_SERVER):

CI4
[SCRIPT_URL] => /verification/activation
[SCRIPT_URI] => http://test.mysite.com/verification/activation
[REQUEST_URI] => /verification/activation

CI3
[SCRIPT_URL] => /dev/verification/activation/
[SCRIPT_URI] => http://test.mysite.com/dev/verification/activation/
[REQUEST_URI] => /dev/verification/activation/

Could this be the problem? Why/how would the trailing slash be removed? (I checked the C++ app and it DOES include the trailing slash in the url to which it posts the data.)
Reply
#8

.htaccess
Code:
    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]
Reply
#9

Yes - I found that last night after I made the previous post. Sadly, changing that made no difference. I am back to square one.
Reply
#10

If you're using Apache as your web server, then commenting out the .htaccess directives makes a difference, and then the backslash at the end of the URI won't matter.

For the test/dev server (php spark serve), the backslash has no effect on route processing.
PHP Code:
$routes->get('form', static function() {
   return 
'<form method="post" action="/form/post/">
        <input type="text" name="a" value="b" />
        <input type="submit" />
</form>'
;
});

$routes->post('form/post', static function() {
   
dd(
       
Services::request()->getPost(),
       
Services::request()->getMethod(),
       
Services::request()->getUri()->getPath(),
   );
}); 
Try checking the routes above with php spark serve
Reply




Theme © iAndrew 2016 - Forum software by © MyBB