CodeIgniter Forums
Capturing post data from C++ application in Codeigniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Capturing post data from C++ application in Codeigniter 4 (/showthread.php?tid=81855)

Pages: 1 2


Capturing post data from C++ application in Codeigniter 4 - junosapien - 05-10-2022

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!


RE: Capturing post data from C++ application in Codeigniter 4 - junosapien - 05-13-2022

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);



RE: Capturing post data from C++ application in Codeigniter 4 - MGatner - 05-15-2022

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));


RE: Capturing post data from C++ application in Codeigniter 4 - MGatner - 05-15-2022

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.


RE: Capturing post data from C++ application in Codeigniter 4 - InsiteFX - 05-16-2022

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"



RE: Capturing post data from C++ application in Codeigniter 4 - junosapien - 05-16-2022

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?


RE: Capturing post data from C++ application in Codeigniter 4 - junosapien - 05-23-2022

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/49192054/php-script-receives-get-instead-of-post-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.)


RE: Capturing post data from C++ application in Codeigniter 4 - iRedds - 05-23-2022

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



RE: Capturing post data from C++ application in Codeigniter 4 - junosapien - 05-24-2022

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


RE: Capturing post data from C++ application in Codeigniter 4 - iRedds - 05-24-2022

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