how to debug webservices?

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Post Reply
spictera
Joomla! Apprentice
Joomla! Apprentice
Posts: 22
Joined: Fri Apr 14, 2023 5:16 pm

how to debug webservices?

Post by spictera » Thu May 25, 2023 3:48 pm

Hi,

I get this error when posting
{"errors":[{"title":"Resource not found","code":404}]}
POST works fine:
* Trying 2607:1b00:93b2:e42c::2667...
* TCP_NODELAY set
* connect to nnnn:nnn:nnnn:nnn port 443 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (nn.nn.nn.nn) port 443 (#0)
* ALPN, offering http/1.1
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
* subject: CN=*.cloudaccess.host
* start date: Dec 14 00:00:00 2022 GMT
* expire date: Nov 22 23:59:59 2023 GMT
* subjectAltName: host "localhost" matched cert's "*.cloudaccess.host"
* issuer: C=US; O=DigiCert, Inc.; CN=RapidSSL Global TLS RSA4096 SHA256 2022 CA1
* SSL certificate verify ok.
> POST /api/index.php/v1/volbilling/staging HTTP/1.1
Host: localhost
Accept: application/vnd.api+json
X-Joomla-Token: secretkey
Content-Length: 839
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 839 out of 839 bytes
But I guess that webservices calls some view, after the data has been POSTed?
As I can read later in the call:
< HTTP/1.1 404 Not Found
< Date: Thu, 25 May 2023 15:38:08 GMT
< Server: Apache
< x-frame-options: SAMEORIGIN
< referrer-policy: strict-origin-when-cross-origin
< cross-origin-opener-policy: same-origin
< Server-Timing: 0Load;dur=65.783978;desc="Load", 1Initialise;dur=559.205055;desc="Initialise", 6ApiRoute;dur=280.481100;desc="ApiRoute", Modules;dur=0;desc="Modules", Access;dur=1.5201568603516;desc="Access"
< X-Powered-By: JoomlaAPI/1.0
< Expires: Wed, 17 Aug 2005 00:00:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Last-Modified: Thu, 25 May 2023 15:38:10 GMT
< Transfer-Encoding: chunked
< Content-Type: application/vnd.api+json; charset=utf-8
<
* Connection #0 to host spictera.cloudaccess.host left intact
{"errors":[{"title":"Resource not found","code":404}]}
But how do I trace the code?
I can't find what is missing?

Here is the api/src/Controller/StagingController.php

Code: Select all

<?php
namespace Spictera\Component\Volbilling\Api\Controller;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\MVC\Controller\ApiController;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Spictera\Component\VolBilling\Administrator\Helper\volbillingHelper;
use Spictera\Component\VolBilling\Administrator\Model\CustomerrolesModel;
use Spictera\Component\VolBilling\Administrator\Model\PartnerrolesModel;

class StagingController extends ApiController
{
        protected $contentType = 'staging';

        protected $default_view = 'staging';

        public function displayList()
        {
                /*
                $apiFilterInfo = $this->input->get('filter', [], 'array');
                $filter = InputFilter::getInstance();

                if(\array_key_exists("cutsomer", $apiFilterInfo))
                {
                        $this->modelState->set('filter.customer_id', $filter->clean($apiFilterInfo['customer_id'],'INT'));
                }

                $apiListInfo = $this->input->get('list',[],'array');

                if(array_key_exists('ordering',$apiListInfo))
                {
                        $this->modelState->set('list.ordering', $filter->clean($apiListInfo['ordering'], 'STRING'));
                }

                if(array_key_exists('direction', $apiListIndo))
                {
                        $this->modelState->set('list.direction', $filter-<clean($apiListInfo['direction'], 'STRING'));
                }
                 */

                return parent::displayList();
        }
        ...
And the views exists for Staging
api/src/View/Staging/JsonapiView.php

Code: Select all

<?php
namespace Spictera\Component\Volbilling\Api\View\Staging;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

class JsonapiView extends BaseApiView
{
        protected $fieldsToRenderItem = ['id', 'alias', 'name', 'catid'];

        protected $fieldsToRenderList = ['id', 'alias', 'name', 'catid'];

        public function displayList(array $items = null)
        {
                foreach (FieldsHelper::getFields('com_volbilling.staging') as $field)
                {
                        $this->fieldsToRenderList[] = $field->id;
                }

                                                $mailer = Factory::getMailer();
                                                $user = Factory::getUser();
                                                $recipient = $user->email;
                                                $mailer->addRecipient($recipient);

                                                $body = "Hi,\n".$content.' '.var_export($items);

                                                $mailer->setSubject('debug add '.$path);
                                                $mailer->setBody($body);
                                                // $mailer->addAttachment(JPATH_COMPONENT.'/assets/document.pdf');

                                                // send the email
                                                $send = $mailer->Send();
                return parent::displayList();
        }
        ...
But the mail never get sent?
And I dont know how to trace this, or review any application logs that can indicate the cause for this.

Any tips?

Regards Tomas

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2571
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: how to debug webservices?

Post by SharkyKZ » Fri May 26, 2023 7:21 am

It's probably another case of misused POST. POST is for creating resources. Use GET to retrieve resources. Review your routes in the plugin. Also code for performing logic like sending emails does not belong in the view.

MarkRS
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 240
Joined: Thu Oct 29, 2009 8:28 am
Location: UK

Re: how to debug webservices?

Post by MarkRS » Fri May 26, 2023 7:23 am

If you can run the webserver on your local machine then you can use a debugger to trace the code.
It's a community, the more we all contribute, the better it will be.

spictera
Joomla! Apprentice
Joomla! Apprentice
Posts: 22
Joined: Fri Apr 14, 2023 5:16 pm

Re: how to debug webservices?

Post by spictera » Sun May 28, 2023 12:54 pm

Thanks,

I resolved the issue by simply disable return a true in the view, as I will not create a view of the POST.
It is a bit strange though that a POST will call the viewer? as a POST suppose to create resources.
But perhaps that is the way how REST works?

Regards Tomas

MarkRS
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 240
Joined: Thu Oct 29, 2009 8:28 am
Location: UK

Re: how to debug webservices?

Post by MarkRS » Sun May 28, 2023 2:11 pm

It's generating a response to the request.

You often need a response, if only a status confirmation.
It's a community, the more we all contribute, the better it will be.


Post Reply

Return to “Joomla! 4.x Coding”