CodeIgniter Forums
Me rindo, no encuentro donde falla el verbo - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Regional User Groups (https://forum.codeigniter.com/forumdisplay.php?fid=25)
+--- Thread: Me rindo, no encuentro donde falla el verbo (/showthread.php?tid=82001)



Me rindo, no encuentro donde falla el verbo - Worked - 06-02-2022

Saludos,
Llevo casi una hora peleandome con el siguiente código, en el que estoy seguro de que el "verbo" es el problema:

He usado la misma ruta para los tres verbos, he cambiado el nombre del controlador por si CodeIgniter usaba el nombre "Enforce" en alguna parte del core, he ... N, pero el resultado es siempre el mismo. GET resuelve sin problemas, pero cuando trato de usar POST o PUT CodeIgniter me devuelve todo el rato al controlador por defecto (login). Estos son los archivos:

PHP Code:
Routes.php
$routes
->get('/(:uuid)/enforce/(:uuid)''Enforce::supplier/$1/$2');
$routes->post('/(:uuid)/enforce/(:uuid)''Enforce::supplier/$1/$2');
$routes->put('/(:uuid)/enforce/(:uuid)''Enforce::supplier/$1/$2'); 

PHP Code:
Enforce.php
<?php

namespace App\Controllers;
use 
CodeIgniter\Controller;

/**
 * Class Enforce
 *
 * Comentarios muy interesantes que no vienen al caso.
 */
class Enforce extends BaseController
{

 
/**
  * Mordaz comentario descriptivo.
  * @param  string  $bla Foo
  * @param  string  $ble Bar
  */
 
public function supplier(string $blastring $ble)
 {

return 
$this->response->setStatusCode(200)
    ->setHeader('Content-Type''application/json')
    ->setBody(json_encode(['Status' => 'danger''Message' => implode(','$_POST)]));
 }
 
// - --  ---  ------------------------------------------------------------

}
/* End of file Enforce.php */
/* Location ./app/Controllers/Enforce.php */ 

Tengo habilitado CSRF, sí, lo tengo habilitado... pero el caso es que en la petición FETCH envío el token y código csrf por lo que no debería ser el problema.

Code:
JavaScript
const Enforce = (elem, form, status) => {
    const article = elem.split('-enforce-')[1]

    const formData = new FormData()
        formData.append('Document', String(document.querySelector('#' + article).dataset.name))
        formData.append('Enforce', String(status))
        formData.append('<?= csrf_token() ?>', '<?= csrf_hash() ?>')
        formData.append('Upload', String(document.querySelector('#' + article).dataset.update))

    fetch(form, {
        method: 'PUT',
        body: formData
    })
    .then(async recv => {
        console.log(recv)
    })
}

Ya estoy tirandome de los pelos... ¿alguna sugerencia?
Gracias.


RE: Me rindo, no encuentro donde falla el verbo - iRedds - 06-02-2022

english please


RE: Me rindo, no encuentro donde falla el verbo - InsiteFX - 06-02-2022

Most new Web Browsers have translate built in now. Chrome right click translate.


RE: Me rindo, no encuentro donde falla el verbo - Worked - 06-03-2022

(06-02-2022, 08:04 PM)iRedds Wrote: english please

Hi.

Quote:Cheers,
I've been struggling for almost an hour with the following code, and I'm pretty sure the problem is in the "verb":

I've used the same path for all three verbs, I've changed the controller name just in case CodeIgniter used the name "Enforce" somewhere in the core, I've try ... N solutions based on my own knowledge and web search, but the result is always the same. GET solves without problems , but when I try to use POST or PUT CodeIgniter returns to the default controller (login) all the time. These are the files:

--- PHP Codes ---

I have CSRF enabled, yes, I have it enabled... but the fact is that in the FETCH request I send the token and csrf code so it shouldn't be the problem (and even if it was for not sending the CSRF token, the truth is that it doesn't even hit the controller)

---- JavaScript Code ---

I'm already driving me crazy... any suggestions?
Thanks.

UPDATE: After posting here, I continued doing a few tests trying to get a clear solution. I've gotten the POST to work over 50% of the cases it hits the controller. I've also gotten the PUT verb to work at 50% too, but using the HTTP spoofing method(1), which doesn't really seem like the best solution to me since it's sending unnecessary parameters and I really wouldn't want to have to fill the API with all those hidden input ="_method".

Thanks

(1) https://codeigniter4.github.io/userguide/incoming/methodspoofing.html