Welcome Guest, Not a member yet? Register   Sign In
Consume API externally
#1

(This post was last modified: 09-28-2021, 08:11 AM by RRIOS.)

I need to use a query using CI4, via an app. I already have the CRUD, but I need it to send the PK through an app and return json.

1- I created a controller that I tested via browser ( htttp://localhost/mycontroller/54265 ) and it's ok.
2- I tested (and obviously) it didn't work to access this controller directly in the browser (http://192.168.0.13/ci4/public/mycontroller)

How through an app (where I usually post www mysite com/myapi/parameter ) will I be able to directly access my controller to return the json?

I did try in Routes :

$routes->add('vercli/(:num)', 'Vercli:: show/$1');

and this

2-$routes->post('vercli/(:num)', 'VerCli:: show/$1);
But it doesn't run the show method. Executes the VerCli index method.

3- I changed the search(code block) from the show($id=null) method to the index($id=null) method and receiving parameter. But the parameter is not recognized ( null ) and change in Routes - $routes->post('vercli/(:num)','VerCli::index/$1');

4- I removed the line above (from the Routes file) result:
nothing changed (it wasn't working).
Reply
#2

You can't test a POST request by typing its url in the browser. This will send a GET request. Use something like Postman if you want to test your API with the right HTTP method.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 10-05-2021, 01:52 PM by RRIOS.)

(10-04-2021, 04:23 PM)includebeer Wrote: You can't test a POST request by typing its url in the browser. This will send a GET request. Use something like Postman if you want to test your API with the right HTTP method.

Hi. Thanks for replying. OK understood.
Maybe I haven't explained it in detail:
I have several CRUD and Querys in a Single Page Application ( codeigniter 3 )
I have some App Android, that use PHP scripts (not inside the Codeigniter structure -others scripts) for query, insert, update and delete.
The app sends the POST or Get and the PHP script returns Json and in the App I handle this return.
Now, I need that, using a crud codeigniter 4 + api (simple queries to 1 table), through these same App, I can do this query and return Json to the App.
I access the method I created with the name show and 1 parameter. But the return is : error 404 + the message I customized. This message is shown in the IF ; after querying the database passing the parameter ID .
The parameter, gets to the show method inside "Vercli" controller as null (I think)
My Codeigniter 4 and Controller Environment , now they are still on localhost.
Reply
#4

Double check your routes, your client app, your .htaccess file, your log files.. You must have something wrong with your routes config or your app doesn't call the right URL with the right HTTP method (POST/GET). Because this is a very simple thing and you must have overlook something.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

(This post was last modified: 10-05-2021, 04:55 PM by RRIOS.)

(10-05-2021, 02:54 PM)includebeer Wrote: Double check your routes, your client app, your .htaccess file, your log files.. You must have something wrong with your routes config or your app doesn't call the right URL with the right HTTP method (POST/GET). Because this is a very simple thing and you must have overlook something.
Thanks for answering

Code:
$routes->get('/', 'Cliente::index');
$routes->post('vercli/show/(:num)','VerCli::show/$1');

Local Apache Access.log ( acess from app in smartphone )

192.168.0.4 - - [05/Oct/2021:17:48:32 -0300] "POST /CI4/public/vercli/show/ HTTP/1.1" 301 250 "-" "Dalvik/2.1.0 (Linux; U; Android 10; moto g(7) play Build/QPYS30.52-22-14)"

192.168.0.4 - - [05/Oct/2021:17:48:33 -0300] "GET /CI4/public/vercli/show HTTP/1.1" 404 114 "-" "Dalvik/2.1.0 (Linux; U; Android 10; moto g(7) play Build/QPYS30.52-22-14)"

Vercli.php Controller
<?php namespace App\Controllers;
 
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
use App\Models\ClienteModel;
 
class VerCli extends ResourceController
{
    use ResponseTrait;

Sorry I couldn't format PHP code separate from the Access Log and separate from the controller PHP code in the forum editor.

THANK YOU, apparently it was app client side issue. 20:55 pm
Reply
#6

(10-05-2021, 04:28 PM)RRIOS Wrote: THANK YOU, apparently it was app client side issue. 20:55 pm

Glad you resolved your problem!  Cool
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB