Trying to create an endpoint to receive API requests but is not working

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

Found my issue (that had since october)

I was writing the “methods” with “s” at the end and is “method”, singular

'routes' => [
    'api' => [
        'mautic_zender_receive_hello' => [
            'path'       => '/zender/receive-hello',
            'controller' => 'MauticPlugin\MauticZenderBundle\Controller\ApiController::receiveHello',
            'method'    => ['POST'],  // Lista de todos los métodos HTTP
            'defaults'   => [],
        ],
    ],
],

This is the correct usage
With the “s” at the end it only accepted the “GET” method

Found very useful this command: php bin/console debug:router > routes.txt
And the solution came from this example: mautic-contact-source/Config/config.php at b8f128a26e8c64fc05bbaf86f7786f7a10f0e1ef · TheDMSGroup/mautic-contact-source · GitHub

This topic was automatically closed 36 hours after the last reply. New replies are no longer allowed.