Not calling custom controller action shows Method not allowed

System Information
  • 4.0:
  • Operating System:
  • Database:
  • Node Version:
  • NPM Version:
  • Yarn Version:

Have added sendEmails action

module.exports = createCoreController(
  "api::application.application",
  ({ strapi }) => ({

    async sendEmails(ctx) {
      console.log("Came inside");
      try {
        console.log(ctx);
        const options = ctx.request.body;
        const user = ctx.state.user;
        if (!user) {
          return ctx.badRequest(null, [
            { messages: [{ id: "No authorization header was found" }] },
          ]);
        }

        //Send email to the user
        await strapi.plugins["email"].services.email.send({
          to: options.to,
          from: options.from,
          subject: options.subject,
          text: options.text,
          html: options.html,
        });

       return ctx.send({
          success: true,
          message: "Email sent successfully",
        });
      } catch (err) {
        ctx.body = err;
        return ctx.send(err);
      }
    },

Calling it below:

const { createCoreRouter } = require("@strapi/strapi").factories;

const defaultRouter = createCoreRouter("api::application.application");

const customRouter = (innerRouter, extraRoutes = []) => {
  let routes;
  return {
    get prefix() {
      return innerRouter.prefix;
    },
    get routes() {
      if (!routes) routes = innerRouter.routes.concat(extraRoutes);
      return routes;
    },
  };
};

const myExtraRoutes = [
  {
    method: "GET",
    path: "/application-details",
    handler:
      "api::application.application.getFormDetailsBasedOnApplicationAction",
  },
  {
    method: "POST",
    path: "/application-send-emails",
    handler: "api::application.application.sendEmails",
  },
];

module.exports = customRouter(defaultRouter, myExtraRoutes);

Error i get:

Hey again Vini,

Check the URL in your postman, you haven’t added /api with the url. The URL should be http://localhost:1337/api/application-send-email

Oh man yes!