3

Having trouble getting the request body in Strapi contoller. I am trying to send email(provider I am using is SendGrid) after user makes a POST request. Trying to make email dynamic.

Post request body:

{
    name:"testname",
    email:"test@test.com",
    type: "normal"
}

I have a route in \config\routes.json

  {
      "method": "POST",
      "path": "/brochure",
      "handler": "brochure.index",
      "config": {
        "policies": []
      }
  } 

In \controllers\brochure.js

const unparsed = require("koa-body/unparsed.js");


module.exports = {
  index: async (ctx) => {
    const unparsedBody = ctx.request.body[unparsed];

    console.log(unparsedBody); // undefined
     await strapi.plugins["email"].services.email.send({
       to: unparsedBody.email,
       from: "test@asd.com",
       subject: "testing Subject",
       text: `Heloooo ${unparsedBody.name}`,
     });
     ctx.send("Email Sent");
  },
};

In \config\middleware.js I have:

module.exports = {
  settings: {
    cors: {
      enabled: true,
    },
    parser: {
      enabled: true,
      multipart: true,
      includeUnparsed: true,
    },
  },
};

Weird thing is that when I am logging ctx.request.body in terminal logs I can see that the POST object with name, email and type are visible, but when logging, for example, ctx.request.body.name it is undefined.

Nikasv
  • 1,317
  • 5
  • 17
  • 33

0 Answers0