codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.4k stars 1.9k forks source link

Bug: getVar() value makes restful endpoint not found #7378

Closed AdeSupriyadi closed 1 year ago

AdeSupriyadi commented 1 year ago

PHP Version

7.4

CodeIgniter4 Version

4.3.2

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

MariaDB 10.2

What happened?

getVar() make restful endpoint not found

Routes

$routes->post('webhook', 'Webhook::create');

Controller

namespace App\Controllers;

use CodeIgniter\API\REsponseTrait;
use CodeIgniter\RESTful\ResourceController;

class Webhook extends ResourceController
{
    use ResponseTrait;
    public function create()
    {
        $vars = $this->request->getVar();
        return $this->respond($vars);  // when it change to return $this->respond('ok'); // its OK
    }
}

call via curl:

curl -i -X POST 'http://localhost:1500/webhook' -H 'Content-Type: application/json' -d '{"id":"123456789"}'

response when controller use return $this->respond($vars);

HTTP/1.1 404 Not Found
Host: localhost:1500
Date: Fri, 24 Mar 2023 07:27:02 GMT
Connection: close
X-Powered-By: PHP/7.4.29
Cache-control: no-store, max-age=0, no-cache
Content-Type: text/html; charset=UTF-8

Steps to Reproduce

I don't know what the problem

Expected Output

API should Respond to data in accordance with the request, http status 200

{"id":"123456789"}

Anything else?

No response

kenjis commented 1 year ago

Cannot reproduce.

--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -31,6 +31,8 @@ $routes->set404Override();
 // route since we don't have to scan directories.
 $routes->get('/', 'Home::index');

+$routes->post('webhook', 'Webhook::create');
+
 /*
  * --------------------------------------------------------------------
  * Additional Routing
<?php

namespace App\Controllers;

use CodeIgniter\API\REsponseTrait;
use CodeIgniter\RESTful\ResourceController;

class Webhook extends ResourceController
{
    use ResponseTrait;

    public function create()
    {
        $vars = $this->request->getVar();

        return $this->respond($vars);  // when it change to return $this->respond('ok'); // its OK
    }
}
$ php spark serve
$ curl -i -X POST 'http://localhost:8080/webhook' -H 'Content-Type: application/json' -d '{"id":"123456789"}'
HTTP/1.1 200 OK
Host: localhost:8080
Date: Fri, 24 Mar 2023 07:55:11 GMT
Connection: close
X-Powered-By: PHP/8.1.16
Cache-Control: no-store, max-age=0, no-cache
Content-Type: application/json; charset=UTF-8
Debugbar-Time: 1679644511.529151
Debugbar-Link: http://localhost:8080/index.php?debugbar_time=1679644511.529151

{
    "id": "123456789"
}
AdeSupriyadi commented 1 year ago

Recreate new app via composer

Still get same response

app\Config\Routes.php

<?php

namespace Config;

// Create a new instance of our RouteCollection class.
$routes = Services::routes();

/*
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
// The Auto Routing (Legacy) is very dangerous. It is easy to create vulnerable apps
// where controller filters or CSRF protection are bypassed.
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
// $routes->setAutoRoute(false);

/*
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');

$routes->post('webhook', 'Webhook::create');

/*
 * --------------------------------------------------------------------
 * Additional Routing
 * --------------------------------------------------------------------
 *
 * There will often be times that you need additional routing and you
 * need it to be able to override any defaults in this file. Environment
 * based routes is one such time. require() additional route files here
 * to make that happen.
 *
 * You will have access to the $routes object within that file without
 * needing to reload it.
 */
if (is_file(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
    require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}

app/Controllers/Webhook.php

<?php
namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
use CodeIgniter\RESTful\ResourceController;

class Webhook extends ResourceController
{
    use ResponseTrait;

    public function create()
    {
        $vars = $this->request->getVar();
        return $this->respond($vars);
    }
}

php spark serve

$ curl -i -X POST 'http://localhost:8080/webhook' -H 'Content-Type: application/json' -d '{"id":"12345353535"}'
HTTP/1.1 404 Not Found
Host: localhost:8080
Date: Fri, 24 Mar 2023 09:03:05 GMT
Connection: close
X-Powered-By: PHP/7.4.29
Cache-control: no-store, max-age=0, no-cache
Content-Type: text/html; charset=UTF-8
php spark serve

CodeIgniter v4.3.2 Command Line Tool - Server Time: 2023-03-24 08:53:44 UTC+00:00

CodeIgniter development server started on http://localhost:8080
Press Control-C to stop.
[Fri Mar 24 15:53:45 2023] PHP 7.4.29 Development Server (http://localhost:8080) started
[Fri Mar 24 15:53:54 2023] [::1]:57137 Accepted
[Fri Mar 24 15:53:54 2023] [::1]:57138 Accepted
[Fri Mar 24 15:53:54 2023] [::1]:57137 Closing
[Fri Mar 24 15:53:55 2023] [::1]:57138 [200]: GET /favicon.ico
[Fri Mar 24 15:53:55 2023] [::1]:57138 Closing
[Fri Mar 24 15:56:26 2023] [::1]:57161 Accepted
[Fri Mar 24 15:56:26 2023] [::1]:57162 Accepted
[Fri Mar 24 15:56:26 2023] [::1]:57161 Closed without sending a request; it was probably just an unused speculative preconnection
[Fri Mar 24 15:56:26 2023] [::1]:57161 Closing
[Fri Mar 24 15:56:26 2023] [::1]:57162 Closing
[Fri Mar 24 15:56:34 2023] [::1]:57163 Accepted
[Fri Mar 24 15:56:34 2023] [::1]:57164 Accepted
[Fri Mar 24 15:56:34 2023] [::1]:57163 Closed without sending a request; it was probably just an unused speculative preconnection
[Fri Mar 24 15:56:34 2023] [::1]:57163 Closing
[Fri Mar 24 15:56:35 2023] [::1]:57164 Closing
[Fri Mar 24 16:03:05 2023] [::1]:57301 Accepted
[Fri Mar 24 16:03:05 2023] [::1]:57301 Closing
datamweb commented 1 year ago

use1 :

C:\Users\ppars>curl -i -X POST 'http://localhost:8080/webhook' -H 'Content-Type: application/json' -d '{"id":"12345353535"}'
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: application

use2:

C:\Users\ppars>curl -i -X POST "http://localhost:8080/webhook" -H 'Content-Type: application/json' -d '{"id":"12345353535"}'
HTTP/1.1 200 OK
Host: localhost:8080
Date: Fri, 24 Mar 2023 09:31:06 GMT
Connection: close
X-Powered-By: PHP/8.1.5
Cache-control: no-store, max-age=0, no-cache
Content-Type: application/json; charset=UTF-8

{"'{id:12345353535}'":""}curl: (6) Could not resolve host: application

use3:

C:\Users\ppars>curl -i -X POST "http://localhost:8080/webhook" -H 'Content-Type:application/json' -d '{"id":"12345353535"}'
HTTP/1.1 200 OK
Host: localhost:8080
Date: Fri, 24 Mar 2023 09:31:44 GMT
Connection: close
X-Powered-By: PHP/8.1.5
Cache-control: no-store, max-age=0, no-cache
Content-Type: application/json; charset=UTF-8

{"'{id:12345353535}'":""}
C:\Users\ppars>
AdeSupriyadi commented 1 year ago

Any one can describe what wrong with my code?

or Codeigniter 4.3.2 should it run on PHP 8? Because the same code run very well in Codeigniter 4.1.5

After try in various php version 7.4.29 Not Working 7.4.30 Working 7.4.33 Working 8.1.5 Not Working 8.1.7 Working 8.2 Working

kenjis commented 1 year ago

I don't know why you get 404, but it seems it is not a bug in CI4.

$ curl -i -X POST 'http://localhost:8080/webhook' -H 'Content-Type: application/json' -d '{"id":"123456789"}'
HTTP/1.1 200 OK
Host: localhost:8080
Date: Fri, 24 Mar 2023 23:00:53 GMT
Connection: close
X-Powered-By: PHP/7.4.33
Cache-Control: no-store, max-age=0, no-cache
Content-Type: application/json; charset=UTF-8
Debugbar-Time: 1679698853.143896
Debugbar-Link: http://localhost:8080/index.php?debugbar_time=1679698853.143896

{
    "id": "123456789"
}
kenjis commented 1 year ago

We use GitHub issues to track BUGS and to track approved DEVELOPMENT work packages. We use our forum to provide SUPPORT and to discuss FEATURE REQUESTS.