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. ----EDIT----- Here is the code from the C app that calls the controller in question:
subscriptioninfo = pSession->OpenRequest(CHttpConnection::HTTP_VERB_POST, address, NULL, NULL, NULL, NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_DONT_CACHE);
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:
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:
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!
I've tried a number of different things, and think the issue is that CI4 is not receiving the Content-Type header from the C app. It is explicitly set with this:
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?
CodePudding user response:
Codeigniter 4 has the following in the public .htaccess file:
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (. )/$
RewriteRule ^ %1 [L,R=301]
which removes the trailing slash from urls. REMming out those three lines
# Redirect Trailing Slashes...
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} (. )/$
# RewriteRule ^ %1 [L,R=301]
leaves the trailing slash in place. And now the C app is able to successfully post to the CI controller.
I don't know yet what can of worms that may open, but it did solve the problem.