Your software
My Mautic version is: 5.1
My PHP version is: 8.2
My Database type and version is: MySQL
Your problem
My problem is: I am trying to create an URI for my endpoint to send data to a plugin I already have but it seems something is wrong because is never really built.
I just want, for now, to send a “hello” request to get a simple answer back but get this error:
[panales@cat ~]$ curl -X POST https://marketautomation.lapañalera.com/api/zender/receive-hello \
-H “Content-Type: application/json”
-H “Authorization: Bearer ZjVjMDJlOTJlNzlmNGQ2MDgxYWVmNTY3YTgxYTFhOGU4ODQ1YzZlYzcwNTZkNjM5OWI3NmNiMzdjMzdjZZZZZZ”
-d ‘{“message”: “hola”}’
{“errors”:[{“message”:“Requested URL not found: /api/zender/receive-hello”,“code”:404,“type”:null}]}[panales@cat ~]$
And
[panales@cat marketautomation]$ php bin/console debug:router | grep receive-hello
mautic_zender_receive_hello GET https ANY /api/zender/receive-hello
[panales@cat marketautomation]$
But the “receive-hello” part appears with red font.
In any case, the URL is not working, have added this to the config file of the plugin:
'routes' => [ 'api' => [ 'mautic_zender_receive_hello' => [ 'path' => '/zender/receive-hello', 'controller' => 'MauticPlugin\MauticZenderBundle\Controller\ApiController::receiveHello', 'methods' => ['POST'], 'defaults' => [], ], ], ],
and /home/panales/public_html/marketautomation/plugins/MauticZenderBundle/Controller/ApiController.php
<?php namespace MauticPlugin\MauticZenderBundle\Controller; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class ApiController extends AbstractController { public function receiveHello(Request $request) { if ($request->getContentType() !== 'json') { throw new BadRequestHttpException('Invalid content type'); } $data = json_decode($request->getContent(), true); if (isset($data['message']) && $data['message'] === 'hola') { return new JsonResponse(['response' => 'recibido']); } return new JsonResponse(['error' => 'Invalid message'], 400); } }
And
/home/panales/public_html/marketautomation/plugins/MauticZenderBundle/Config/services.yaml
services:
MauticPlugin\MauticZenderBundle\Controller\ApiController:
public: true
tags: [‘controller.service_arguments’]
Mautic is not crashing at least and the rest of the plugin works as intended, but can’t get the API POST requests because the endpoint looks like it doesn’t exist.
This is the structure of the plugin
[panales@cat MauticZenderBundle]$ tree
.
├── Assets
│ └── img
│ ├── 7cats-isotipo-red-200x200.png
│ └── whatsapp.png
├── Config
│ ├── config.php
│ └── services.yaml
├── Controller
│ └── ApiController.php
├── EventListener
│ └── PluginActivatedEventListener.php
├── Integration
│ └── ZenderIntegration.php
├── LICENSE
├── MauticZenderBundle.php
├── Migrations
│ └── Schema
│ └── Version0001.php
├── README.md
├── Translations
│ └── en_US
│ └── messages.ini
└── Transport
└── ZenderTransport.php
11 directories, 13 files
These errors are showing in the log:
Steps I have tried to fix the problem: Read the documentation but find no specific reference to create an endpoint in a Plugin